先说一下思路:先获取屏幕截屏图片,然后对该图片进行模糊化处理等操作得到背景图,覆盖到当前窗口上面。
最开始使用iOS8自带毛玻璃效果UIVisualEffectView,但是发现效果不好。思考怎么设置模糊的程度尝试了更改blurView的alpha值,但是效果仍不理想。之后查找到另外几种方法:CoreImage.framework的Core Image和Accelerate.framework的vImage,还有一些第三方的库GPUImage和UIImage+ImageEffects等。最终使用了基于vImage的UIImage+ImageEffects。
代码如下:
- (void)btn_click{
UIImageView *blur_imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
blur_imgv.image = [[self screenShotImage] applyBlurWithRadius:10 tintColor:[UIColor colorWithWhite:0.8 alpha:0.3] saturationDeltaFactor:1.0 maskImage:nil];
[self.viewaddSubview:blur_imgv];
}
- (UIImage*)screenShotImage{
UIWindow *screenWindow = [[UIApplication sharedApplication]keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnviewImage;
}
效果如下:
网友评论