关于私密性的app,需要做的后台模糊效果

我们经常使用手机会发现,支付宝,银行类的,包括现在的私密社交类的app,后台运行的时候会把当前界面模糊掉

下面是实现方式

需要用到的资料 

UIImage+ImageEffects.h

参考链接

下面是代码部分

在 AppDelegate 文件中声明

模糊截图对象

@property (strong, nonatomic) UIImageView *blurView;

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    self.blurView = [[UIImageView alloc] initWithImage:[self makeBlurredScreenshot:self.window.rootViewController.view.frame]];
    self.blurView.frame = self.window.rootViewController.view.frame;
    [self.window.rootViewController.view addSubview:self.blurView];
}

- (UIImage *)makeBlurredScreenshot:(CGRect)frame {
    
    UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0.0);
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextConcatCTM(c, CGAffineTransformMakeTranslation(-frame.origin.x, -frame.origin.y));
    [[[[UIApplication sharedApplication] keyWindow] layer] renderInContext:c];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [screenshot applyBlurWithRadius:5
                                 tintColor:[UIColor colorWithWhite:1.0 alpha:0.3]
                     saturationDeltaFactor:1.8
                                 maskImage:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    if (self.blurView) {
        [self.blurView removeFromSuperview];
        self.blurView = nil;
    }
}


你可能感兴趣的:(关于私密性的app,需要做的后台模糊效果)