工作生活

iOS App页面切换保护(敏感数据进行高斯模糊处理)

2019-07-02  本文已影响0人  Uncle_Gao

这里有借鉴其他人的方法,整理并总结下对app页面进行高斯模糊处理的方法。
效果图分别对应方法一、方法二


image.png

方法一、
这种方式加了一层毛玻璃效果,能达到目的,但是效果并不十分理想.

- (void)applicationWillResignActive:(UIApplication *)application {
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visualEffectView.frame = self.window.bounds;
    visualEffectView.alpha = 0.92;
    self.visualEffectView = visualEffectView;
    [self.window addSubview:visualEffectView];
    
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    self.visualEffectView.alpha = 0;
    [self.visualEffectView removeFromSuperview];
}

方法二、(首先把固定区域的view转换成图片, 然后将图片进行高斯模糊, 盖在原来的view上.)
1.将固定区域的view转换成图片;两个参数, 一个传入想转换成图片的view, 另一个传想转成图片的size;

+ (UIImage *)tg_makeImageWithView:(UIView *)view withSize:(CGSize)size {
    // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

2.对图片做高斯模糊;先将UIImage转换成CIImage, 然后设置滤镜, 给滤镜的@"inputRadius"的值设置value进行高斯模糊, 将CIImage转换成UIImage返回.

+ (UIImage *)tg_blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if (image == nil) {
        return nil;
    }
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:ciImage forKey:kCIInputImageKey];
    //设置模糊程度
    [filter setValue:@(blur) forKey: @"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGImageRef outImage = [context createCGImage: result fromRect:ciImage.extent];
    UIImage * blurImage = [UIImage imageWithCGImage:outImage];
    CGImageRelease(outImage);
    return blurImage;
}

实例调用方法如下(blur值越大,模糊效果越明显):

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    self.bgimg = [[UIImageView alloc] initWithImage:[AppDelegate tg_blurryImage:[AppDelegate tg_makeImageWithView:self.window withSize:CGSizeMake(kSCREEN_WIDTH, kSCREENH_HEIGHT)] withBlurLevel:20]];
    self.bgimg.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREENH_HEIGHT);
    [self.window addSubview:self.bgimg];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
   
    [self.bgimg removeFromSuperview];
}
上一篇 下一篇

猜你喜欢

热点阅读