iOS安全-切換后臺背景模糊
導讀
我們在雙擊切換到后臺的時候,背景會有縮略圖,而縮略圖會暴漏用戶的部分敏感數據,所以要求切換到后臺后,縮略圖需要做毛玻璃模糊處理。一些銀行類的應用會要求這么做。比如招商銀行。
切換到后臺效果,可以看到時鐘是沒有模糊的。
實現思路也很簡單在切換后臺前,截取當前頁面,然后做高斯模糊,然后加在window上。切到前臺前,將這個加的頁面移除出去。
關鍵代碼:
//
// PAFFBlurryView.m
// Pods
//
// Created by bolei on 16/10/9.
//
//
#import "BLBlurryView.h"
#import <Accelerate/Accelerate.h>
#define kBlurryTag 10009
@implementation BLBlurryView
- (instancetype)initWithFrame:(CGRect)frame blurryView:(UIView *)view {
self = [super initWithFrame:frame];
if (self) {
UIImage *sourceImage = [self getCurrentImageWithView:view];
if (sourceImage) {
UIImage *image = [UIImage imageWithData:UIImageJPEGRepresentation(sourceImage, 1.0)];
UIImage *sImage = [self blurryImage:image withBlurLevel:0.1];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:frame];
bgView.image = sImage;
[self addSubview:bgView];
}
}
return self;
}
+ (void)showBlurryViewInWindow {
UIWindow *window = [UIApplication sharedApplication].delegate.window;
BLBlurryView *view = [[BLBlurryView alloc] initWithFrame:window.frame blurryView:nil];
view.tag = kBlurryTag;
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if (window.windowLevel == UIWindowLevelNormal) {
UIView *preView = [window viewWithTag:kBlurryTag];
if (preView) {
[preView removeFromSuperview];
}
[window addSubview:view];
}
}
}
+ (void)removeBlurryViewInWindow {
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if (window.windowLevel == UIWindowLevelNormal) {
UIView *view = [window viewWithTag:kBlurryTag];
if (view) {
[view removeFromSuperview];
}
}
}
}
- (UIImage *)getCurrentImageWithView:(UIView *)view
{
UIView *sourceView = view;
if (sourceView == nil) {
sourceView = [self getCurrentVisibleView];
}
if (sourceView == nil) {
return nil;
}
UIGraphicsBeginImageContext(sourceView.bounds.size);
[sourceView.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);
if (error) {
NSLog(@"error from convolution %ld", error);
}
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];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
- (UIView *)getCurrentVisibleView {
UIWindow *window = [[UIApplication sharedApplication].delegate window];
UIViewController *rootViewController = window.rootViewController;
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
return ((UINavigationController *)rootViewController).visibleViewController.view;
}
return rootViewController.view;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
邏輯并不復雜,主要麻煩的點在于什么時候調用,切到后臺涉及到四個周期:
- applicationWillResignActive 失去焦點的時候會首先調用
- applicationDidEnterBackground 切換到后臺后調用。在WillResignActive之后調用
- applicationWillEnterForeground 將要回到前臺的時候調用
- applicationDidBecomeActive 已經到前臺后調用
其中點擊home,切換到后臺,四個狀態都會調用。但是雙擊home的話只會調用applicationWillResignActive和applicationDidBecomeActive
一般調用顯示和消失需要成對調用。所以兩個方案:
- 方案1:applicationWillResignActive調用消失,然后applicationWillEnterForeground調用顯示
- 方案2:applicationDidEnterBackground調用消失,然后applicationDidBecomeActive調用顯示
方案1的問題是:在調用applicationWillResignActive,需要截屏需要做模糊效果,如果這個調用時間過長,就不會顯示模糊的效果,對于復雜的頁面經常會出現這個問題,需要找更好的算法去解決,目前沒找到好的方案。而且進入到APP會有比較久的時間的模糊效果。
方案2的問題是:在雙擊home的時候,這個模糊效果不會生效。相對來說更推薦方案二。