现在很多设计中都用到了模糊效果,或当背景,或实现渐变等等,模糊效果的应用范围已经变的越来越广泛。在这里介绍两种模糊效果,毛玻璃与高斯模糊。
首先,介绍一种iOS 8 以后系统自带的毛玻璃效果。(顾名思义这种效果并不是对图片本身操作,所以使用要注意)
self.imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
_imageV.image = [UIImage imageNamed:@"1.jpg"];
_imageV.clipsToBounds = YES;
_imageV.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageV];
// 创建需要的毛玻璃特效类型
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// 毛玻璃视图
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
effectView.frame = _imageV.bounds;
effectView.alpha = 0.8f;
[_imageV addSubview:effectView];
下面,使用CoreImage 来实现高斯模糊效果:
CIContext *context = [CIContext contextWithOptions:nil];CIImage *inputImage = [CIImage imageWithCGImage:_imageV.image.CGImage];
CGImageRef renderImage;
CIImage *filtered = [inputImage imageByClampingToExtent];
filtered = [filtered imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey:@20}];
filtered = [filtered imageByCroppingToRect:inputImage.extent];
renderImage = [context createCGImage:filtered fromRect:inputImage.extent];
UIImage * blurImage = [UIImage imageWithCGImage:renderImage];
_imageV.image = blurImage;
其中,CIImage是CoreImage框架中最基本代表图像的对象,他不仅包含元图像数据,还包含作用在原图像上的滤镜链。这里我想特别强调的是CIImage和其他图像是不同的,在CIImage被CIContext渲染出来之前,他是依赖于滤镜链的,滤镜是不会更改CIImage中的图像数据。这个需要正确理解,不然会给你的程序造成错误。说到了CIImage的不同,就必须得提一下如何创建CIImage了,CIImage是不能直接有UIImage转化而来的。