当应用进入后台时,系统会自动在当前应用的页面截屏并存储到手机内,如果当前页面涉及敏感信息时,被攻击会造成泄密。如下图生成的两张图片路径:
防止造成信息泄露 - 添加高斯模糊(参考支付宝挂起时的模糊效果)
实现方案:
在applicationWillResignActive中添加模糊视图
- (BlurView *)backgroundBar{
if (!_backgroundBar) {
_backgroundBar = [[BlurView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
return _backgroundBar;
}
- (void)applicationWillResignActive:(UIApplication *)application {
__weak typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if (window.windowLevel == UIWindowLevelNormal) {
[window addSubview:weakSelf.backgroundBar];
}
}
});
}
然后在applicationDidBecomeActive中将模糊视图删除
- (void)applicationDidBecomeActive:(UIApplication *)application {
__weak typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.backgroundBar removeFromSuperview];
weakSelf.backgroundBar = nil;
});
}
模糊视图实现方案
#import "CMFKBlurView.h"
#import <Accelerate/Accelerate.h>
#define screenWidth SCREEN_WIDTH
#define screenHeight SCREEN_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 CMFKBlurView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
UIImage *image = [UIImage imageWithData:UIImageJPEGRepresentation([self getCurrentImage], 1.0)];
self.blurImage = [self blurryImage:image withBlurLevel:0.3];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:frame];
bgView.image =self.blurImage;
[self addSubview:bgView];
}
return self;
}
- (UIImage *)getCurrentImage{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(screenWidth*screenScale,screenHeight*screenScale),YES, 0);//截屏
[[[[UIApplication sharedApplication] 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 = [[UIImage alloc] 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 = [UIImage imageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
参考文章: https://blog.youkuaiyun.com/jp940110jpjp/article/details/44307515