以上个界面为背景的新界面的模糊处理

分享个小玩意

最近有些小需求:要以上个界面为背景在新的界面添加毛玻璃效果(例如登录页面,在呼出登录界面的时候,要将上个界面做为背景,以毛玻璃效果添加在上面);

以上个界面为背景的新界面的模糊处理_第1张图片

由于弹出的页面是push出来的Controller,所以难以将整个界面设置成半透明效果(效果不好),而且添加毛玻璃效果的时候需要有个原始图片,因此这里采取了在弹出页面时先将上个屏幕截屏 获得一个截图传给弹出视图的方案。

实现起来很简单

ShowViewController *vc = [[ShowViewController alloc]init];

vc.shotImage = [UIImage screenShotInView:self.view];//截图

[self.navigationController pushViewController:vc animated:YES];


截图功能放在了UIImage+ZWColorToImage.h 类别里

/**

*截图功能  传入需要截屏的view

*/

+ (UIImage *)screenShotInView:(UIView *)view

{

UIGraphicsBeginImageContext(view.frame.size);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * shotImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRef shotRef = shotImage.CGImage;

CGImageRef ResultRef = CGImageCreateWithImageInRect(shotRef, view.frame);

UIImage * Result = [UIImage imageWithCGImage:ResultRef];

CGImageRelease(ResultRef);

return Result;

}

截屏过后将图片传入 需要弹出的页面 然后在添加 毛玻璃效果就OK了

//毛玻璃

self.bgImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

UIImage *sourceImage = self.shotImage;//图片源 (上个界面的截图)

UIImage *lastImage = [sourceImage applyLightEffect];//添加模糊效果

self.bgImageView.image = lastImage;

self.bgImageView.userInteractionEnabled = YES;

[self.view addSubview:self.bgImageView];

demo:https://github.com/STRBIGBEAR/XTQBlurEffectTest.git

你可能感兴趣的:(以上个界面为背景的新界面的模糊处理)