這里有借鑒其他人的方法,整理并總結(jié)下對(duì)app頁面進(jìn)行高斯模糊處理的方法捷雕。
效果圖分別對(duì)應(yīng)方法一叁鉴、方法二
方法一、
這種方式加了一層毛玻璃效果,能達(dá)到目的,但是效果并不十分理想.
- (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];
}
方法二江醇、(首先把固定區(qū)域的view轉(zhuǎn)換成圖片, 然后將圖片進(jìn)行高斯模糊, 蓋在原來的view上.)
1.將固定區(qū)域的view轉(zhuǎn)換成圖片;兩個(gè)參數(shù), 一個(gè)傳入想轉(zhuǎn)換成圖片的view, 另一個(gè)傳想轉(zhuǎn)成圖片的size濒憋;
+ (UIImage *)tg_makeImageWithView:(UIView *)view withSize:(CGSize)size {
// 下面方法,第一個(gè)參數(shù)表示區(qū)域大小陶夜。第二個(gè)參數(shù)表示是否是非透明的凛驮。如果需要顯示半透明效果,需要傳NO条辟,否則傳YES黔夭。第三個(gè)參數(shù)就是屏幕密度了,關(guān)鍵就是第三個(gè)參數(shù) [UIScreen mainScreen].scale羽嫡。
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2.對(duì)圖片做高斯模糊;先將UIImage轉(zhuǎn)換成CIImage, 然后設(shè)置濾鏡, 給濾鏡的@"inputRadius"的值設(shè)置value進(jìn)行高斯模糊, 將CIImage轉(zhuǎn)換成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];
//設(shè)置模糊程度
[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;
}
實(shí)例調(diào)用方法如下(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];
}