應用程序加毛玻璃蒙版保護帳戶信息

廢話很少說,先來看看沒有蒙版的效果 ,再來看看有蒙版的效果git

 ,明顯能夠看出有明顯的區別。緩存

先說說實現的的思路:app

當程序即將進入後臺時,把當前屏幕截個圖,此時要將圖片毛玻璃化,並做爲UIImageView的image,而後將imageView放在window的最上面,等即將進入前臺時移除毛玻璃蒙版。ide

下面就來講代碼實現吧(在代理文件中實現):atom

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

 主要代碼:spa

 

- (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調用。3d

//加模糊效果,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代理

相關文章
相關標籤/搜索