最近公司要做类似支付宝那种程序进入后台,再打开后程序界面变得模糊,网上找到了一份代码在此分享出来

实现模糊的代码

.h文件

#import <UIKit/UIKit.h>

 

@interface BlurredView : UIView

 

@end

 

 

.m文件

#import "BlurredView.h"

#import <Accelerate/Accelerate.h>

 

@implementation BlurredView

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        UIImage *image = [UIImage imageWithData:UIImageJPEGRepresentation([self getCurrentImage], 1.0)];

        UIImage *sImage =  [self blurryImage:image withBlurLevel:0.1];

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

        bgView.image = sImage;

        [self addSubview:bgView];

    }

    return self;

}

 

- (UIImage *)getCurrentImage

{

    UIWindow *window = [[UIApplication sharedApplication].delegate window];

    UIGraphicsBeginImageContext(window.rootViewController.view.bounds.size);

    [window.rootViewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

 

    UIGraphicsEndImageContext();

    return image;

}

 

- (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);

    

    

   

    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef ctx = CGBitmapContextCreate(

                                             outBuffer.data,

                                             outBuffer.width,

                                             outBuffer.height,

                                             8,

                                             outBuffer.rowBytes,

                                             colorSpace,

                                             kCGImageAlphaNoneSkipLast);

    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

    

    

    CGContextRelease(ctx);

    CGColorSpaceRelease(colorSpace);

    

    free(pixelBuffer);

    CFRelease(inBitmapData);

    

    CGColorSpaceRelease(colorSpace);

    CGImageRelease(imageRef);

    

    return returnImage;

}

@end

 

然后在AppDelegate中这两个方法中实现就好了

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    //添加

    BlurredView *view = [[BlurredView alloc] initWithFrame:self.window.frame];

    view.tag = 12345;

    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {

        if (window.windowLevel == UIWindowLevelNormal) {

            [window addSubview:view];

        }

    }

 }

 

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    //移除

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    for (window in [[UIApplication sharedApplication] windows]) {

        if (window.windowLevel == UIWindowLevelNormal) {

            UIView *view = [window viewWithTag:12345];

            [view removeFromSuperview];

        }

    }

 }

 

posted on 2015-04-08 15:52  ABCDEFGHIJKLMN  阅读(305)  评论(0编辑  收藏  举报