iOS進入后台模糊界面
- 兩種方式,一種是截取當前界面,做高斯模糊
- 一種是直接搞一個模糊界面蓋在window最上層,推薦這種,方便性能好

1、截取當前界面模糊
- 這種方法比較耗性能,畢竟要截圖,查找window,再模糊處理。
- (void)screenShot {
// 保證每次截取的都是最新的界面
if(self.screenShotImageV) {
[self.screenShotImageV removeFromSuperview];
}
CGSize imageSize = CGSizeZero;
//屏幕朝向
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation))
imageSize = [UIScreen mainScreen].bounds.size;
else
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//按理應取用戶看見的那個window
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, 0, -imageSize.width);
}
else if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
}
else
{
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
}
//截屏圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//截屏圖片t處理后
UIImage *gaussianImage = [self coreGaussianBlurImage:image blurNumber:30];
//生成控件
UIImageView *bgImgv = [[UIImageView alloc] initWithImage:gaussianImage];
bgImgv.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
self.screenShotImageV = bgImgv;
}
/**
高斯模糊 處理
@param image 要處理的image
@param blur 模糊度
@return 處理后的image
*/
- (UIImage *)coreGaussianBlurImage:(UIImage * _Nonnull)image blurNumber:(CGFloat)blur {
if (!image) {
return nil;
}
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:blur] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
UIImage *blurImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return blurImage;
}
//顯示window
- (void)show {
[self screenShot];
[[UIApplication sharedApplication].delegate.window addSubview:self.screenShotImageV];
[[UIApplication sharedApplication].delegate.window bringSubviewToFront:self.screenShotImageV];
}
//隱藏window
- (void)hidden {
if(self.screenShotImageV) {
[self.screenShotImageV removeFromSuperview];
}
}
2、通用界面模糊
- 這種比較方便,只需要創建一個界面即可,和具體界面無關。
/**
打開后台快照遮罩
*/
- (void)executePresentBlurSnapshot
{
if (!self.effectView) {
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
self.effectView = effectView;
}
[[UIApplication sharedApplication].delegate.window addSubview:self.effectView];
[[UIApplication sharedApplication].delegate.window bringSubviewToFront:self.effectView];
}
/**
關閉后台快照遮罩
*/
- (void)executeDismissBlurSnapshot
{
if (self.effectView) {
[self.effectView removeFromSuperview];
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] show];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] hidden];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] hidden];
}