类似于支付宝双击home键进入后台模糊效果-blurView

来源:互联网 发布:mac怎么打开html文件 编辑:程序博客网 时间:2024/06/05 16:39

废话少说直接代码

1.入口类里

#import "AppDelegate.h"

#import "JPBlurView.h"

- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

//    [[NSNotificationCenter defaultCenter]postNotificationName:@"back" object:nil];

    //双击进入后台

    JPBlurView *view = [[JPBlurViewalloc]initWithFrame:self.window.frame];

    view.tag =1111111;


    for (UIWindow *windowin [[UIApplicationsharedApplication]windows]) {

        if (window.windowLevel ==UIWindowLevelNormal) {

            [window addSubview:view];

        }

    }


}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


    JPBlurView *view = [[JPBlurViewalloc]initWithFrame:self.window.frame];

    view.tag =1111111;

    for (UIWindow *windowin [[UIApplicationsharedApplication]windows]) {

        if (window.windowLevel ==UIWindowLevelNormal) {

            [window addSubview:view];

        }

    }


}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    //移除

    for (UIWindow *windowin [[UIApplicationsharedApplication]windows]) {

        if (window.windowLevel ==UIWindowLevelNormal) {

            UIView *view = [windowviewWithTag:1111111];

            [view removeFromSuperview];

        }

    }


}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    //移除

    for (UIWindow *windowin [[UIApplicationsharedApplication]windows]) {

        if (window.windowLevel ==UIWindowLevelNormal) {

            UIView *view = [windowviewWithTag:1111111];

            [view removeFromSuperview];

        }

    }


}


2.JPBlurView

JPBlurView.h

#import <UIKit/UIKit.h>


@interface JPBlurView : UIView

@property(nonatomic,retain)UIImage *blurImage;

@end


JPBlurView.m

#import "JPBlurView.h"

#import <Accelerate/Accelerate.h>

#define screenWidth ([UIScreen mainScreen].bounds.size.width)

#define screenHeight ([UIScreen mainScreen].bounds.size.height)

#define screenScale ([UIScreen mainScreen].scale)


#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

#define kCGImageAlphaPremultipliedLast  (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)

#else

#define kCGImageAlphaPremultipliedLast  kCGImageAlphaPremultipliedLast

#endif

@implementation JPBlurView


- (id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        

        UIImage *image = [UIImageimageWithData:UIImageJPEGRepresentation([selfgetCurrentImage],1.0)];

        self.blurImage =  [selfblurryImage:imagewithBlurLevel:0.3];

        UIImageView *bgView = [[UIImageViewalloc]initWithFrame:frame];

        bgView.image =self.blurImage;

        [selfaddSubview:bgView];

    }

    returnself;

}

- (UIImage *)getCurrentImage

{

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(screenWidth*screenScale,screenHeight*screenScale),YES,0);

    //截屏

    [[[[UIApplicationsharedApplication]keyWindow]layer]renderInContext:UIGraphicsGetCurrentContext()];

    

    UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    CGImageRef imageRef = viewImage.CGImage;

    CGRect rect =CGRectMake(0,0,screenWidth*screenScale,screenHeight*screenScale);

    

    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

    UIImage *sendImage = [[UIImagealloc]initWithCGImage:imageRefRect];

    CGImageRelease(imageRefRect);

    return sendImage;

    

}

//模糊算法

- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {

    if (blur <0.f || blur >1.f) {

        blur = 0.5f;

    }

    int boxSize = (int)(blur *100);

    boxSize = boxSize - (boxSize % 2) +1;

    

    CGImageRef img = image.CGImage;

    

    vImage_Buffer inBuffer, outBuffer;

    vImage_Error error;

    

    void *pixelBuffer;

    

    CGDataProviderRef inProvider =CGImageGetDataProvider(img);

    CFDataRef inBitmapData =CGDataProviderCopyData(inProvider);

    

    inBuffer.width =CGImageGetWidth(img);

    inBuffer.height =CGImageGetHeight(img);

    inBuffer.rowBytes =CGImageGetBytesPerRow(img);

    

    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    

    pixelBuffer = malloc(CGImageGetBytesPerRow(img) *

                         CGImageGetHeight(img));

    

    if(pixelBuffer ==NULL)

        NSLog(@"No pixelbuffer");

    

    outBuffer.data = pixelBuffer;

    outBuffer.width =CGImageGetWidth(img);

    outBuffer.height =CGImageGetHeight(img);

    outBuffer.rowBytes =CGImageGetBytesPerRow(img);

    

    error = vImageBoxConvolve_ARGB8888(&inBuffer,

                                       &outBuffer,

                                       NULL,

                                       0,

                                       0,

                                       boxSize,

                                       boxSize,

                                       NULL,

                                       kvImageEdgeExtend);

    

    

    if (error) {

        NSLog(@"error from convolution %ld", error);

    }

    

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    CGContextRef ctx =CGBitmapContextCreate(

                                             outBuffer.data,

                                             outBuffer.width,

                                             outBuffer.height,

                                             8,

                                             outBuffer.rowBytes,

                                             colorSpace,kCGImageAlphaPremultipliedLast );

    CGImageRef imageRef =CGBitmapContextCreateImage (ctx);

    UIImage *returnImage = [UIImageimageWithCGImage:imageRef];

    

    //clean up

    CGContextRelease(ctx);

    CGColorSpaceRelease(colorSpace);

    

    free(pixelBuffer);

    CFRelease(inBitmapData);

    

    CGColorSpaceRelease(colorSpace);

    CGImageRelease(imageRef);

    

    return returnImage;

}

@end


0 0
原创粉丝点击