码迷,mamicode.com
首页 > 其他好文 > 详细

应用程序加毛玻璃蒙版保护账户信息

时间:2014-12-09 17:23:45      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   sp   for   

废话不多说,先来看看没有蒙版的效果bubuko.com,布布扣 bubuko.com,布布扣,再来看看有蒙版的效果

bubuko.com,布布扣 bubuko.com,布布扣,明显可以看出有明显的区别。

先说说实现的的思路:

当程序即将进入后台时,把当前屏幕截个图,此时要将图片毛玻璃化,并作为UIImageView的image,然后将imageView放在window的最上面,等即将进入前台时移除毛玻璃蒙版。

下面就来说代码实现吧(在代理文件中实现):

@interface AppDelegate ()
@property (nonatomic, weak)UIImageView *cover;
@end

 主要代码:

 

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    self.cover = imgView;
    [[UIApplication sharedApplication].keyWindow addSubview:imgView];

    UIGraphicsBeginImageContext([UIApplication sharedApplication].keyWindow.size);
    // 将当前layer的截图写进上下中
    [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    // 从上下中读取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  // 压缩图片,并转成JPEG类型
    NSData *imageData = UIImageJPEGRepresentation(image, 0.01);

    image = [UIImage imageWithData:imageData];
  // 注意:不能直接将图片毛玻璃化,要转成data后,再转成image,此时的image才能毛玻璃化,不然调用blurryImage:withBlurLevel会报错。
    imgView.image = [UIImage blurryImage:image withBlurLevel:0.15];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    [self.cover removeFromSuperview];
}

 

 

 

//下面代码是 blurryImage:withBlurLeve方法l的具体实现,我将它写成了UIImage的分类,所以可以直接用UIImage调用。

//加模糊效果,image是图片,blur是模糊度
+ (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    //模糊度,
    if ((blur < 0.1f) || (blur > 2.0f)) {
        blur = 0.5f;
    }
    
    //boxSize必须大于0
    int boxSize = (int)(blur * 100);
    boxSize -= (boxSize % 2) + 1;
    NSLog(@"boxSize:%i",boxSize);
    //图像处理
    CGImageRef img = image.CGImage;
    //需要引入#import <Accelerate/Accelerate.h>
    /*
     This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing.
     本文档介绍了Accelerate Framework,其中包含C语言应用程序接口(API)的向量和矩阵数学,数字信号处理,大量处理和图像处理。
     */
    
    //图像缓存,输入缓存,输出缓存
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    //像素缓存
    void *pixelBuffer;
    
    //数据源提供者,Defines an opaque type that supplies Quartz with data.
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    // provider’s data.
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    
    //宽,高,字节/行,data
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
    
    //像数缓存,字节行*图片高
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    
    // 第三个中间的缓存区,抗锯齿的效果
    void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    vImage_Buffer outBuffer2;
    outBuffer2.data = pixelBuffer2;
    outBuffer2.width = CGImageGetWidth(img);
    outBuffer2.height = CGImageGetHeight(img);
    outBuffer2.rowBytes = CGImageGetBytesPerRow(img);
    
    //Convolves a region of interest within an ARGB8888 source image by an implicit M x N kernel that has the effect of a box filter.
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    
    
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
    
    //    NSLog(@"字节组成部分:%zu",CGImageGetBitsPerComponent(img));
    //颜色空间DeviceRGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //用图片创建上下文,CGImageGetBitsPerComponent(img),7,8
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             CGImageGetBitmapInfo(image.CGImage));
    
    //根据上下文,处理过的图片,重新组件
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    
    free(pixelBuffer);
    free(pixelBuffer2);
    CFRelease(inBitmapData);
    
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    
    return returnImage;
}

Code4App里有详细的Demo:http://code4app.com/member/53908169933bf0c8238b4d43

 

应用程序加毛玻璃蒙版保护账户信息

标签:des   style   blog   http   io   ar   color   sp   for   

原文地址:http://www.cnblogs.com/Fc-ios/p/4153401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!