iOS 信息安全 - 后台挂起

当iOS应用进入后台时,系统保存的屏幕快照可能暴露敏感信息。本文介绍了如何通过添加高斯模糊视图来防止信息泄露,详细讲解了在`applicationWillResignActive`和`applicationDidBecomeActive`中添加和移除模糊视图的实现,并提供了模糊算法的实现代码。参考了支付宝后台挂起时的模糊效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当应用进入后台时,系统会自动在当前应用的页面截屏并存储到手机内,如果当前页面涉及敏感信息时,被攻击会造成泄密。如下图生成的两张图片路径:

 

防止造成信息泄露 - 添加高斯模糊(参考支付宝挂起时的模糊效果)

实现方案:

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值