iOS进入后台模糊界面
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];
}
}
- 在delegate控制显示和隐藏
- (void)applicationWillResignActive:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] show];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] hidden];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[LQAboveBackgroundView sharedInstance] hidden];
}
本文来自博客园,作者:struggle_time,转载请注明原文链接:https://www.cnblogs.com/songliquan/p/16038774.html