//1.模糊毛玻璃效果实现方案一
//利用系统的CoreImage(滤镜) 滤镜处理的过程比较慢,会造成加载图片缓慢的现象(等一会才看到图片),尽量放到子线程执行
UIImage *image = [UIImage imageNamed:@"1_1280x800"];
//1.创建CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:image];
//2.创建滤镜CIFilter
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
//3.将CIImage输入到滤镜中
[blurFilter setValue:ciImage forKey:kCIInputImageKey];
//4.设置模糊度
[blurFilter setValue:@(10) forKey:@"inputRadius"];
//5.将处理好的图片输出
CIImage *outCiImage = [blurFilter valueForKey:kCIOutputImageKey];
//6.CIContext (option 参数为nil代表用CPU渲染,若想用GPU渲染请查看此参数)
CIContext *context = [CIContext contextWithOptions:nil];
//7.获取CGImage句柄
CGImageRef outCGImage = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
//8.获取最终的图片
UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
//9.释放CGImage
CGImageRelease(outCGImage);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image = blurImage;
[self.view addSubview:imageView];
//方案二:利用UIImage + ImageEffects 分类 下载地址:https://github.com/YouXianMing/UIImageBlur
UIImage *sourceImage = [UIImage imageNamed:@"1_1280x800"];
//设置模糊度
UIImage *blurImage = [sourceImage blurImageWithRadius:10];
//设置局部模糊度
//UIImage * blurImage2 = [image blurImageAtFrame:CGRectMake(0, 0, image.size.width, image.size.height/2)];
//设置灰度效果
//UIImage * blurImage3 = [image grayScale];
/*其他的一些方法,可以自己尝试使用
固定宽度与固定高度
- (UIImage *)scaleWithFixedWidth:(CGFloat)width;
- (UIImage *)scaleWithFixedHeight:(CGFloat)height;
平均的颜色
- (UIColor *)averageColor;
裁剪图片的一部分
- (UIImage *)croppedImageAtFrame:(CGRect)frame;
*/
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.image = blurImage;
[self.view addSubview:imageView];
//方案三:利用UIVisualEffectView(iOS8)
// 添加展示的背景,用于显示动态模糊(背景能够滚动,便于查看动态的模糊)
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1_1280x800"]];
self.scrollView.contentSize = imageV.image.size;
self.scrollView.bounces = NO;
[self.scrollView addSubview:imageV];
[self.view addSubview:self.scrollView];
//添加模糊效果
// 1.创建模糊view
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
// 2.设定模糊View的尺寸
effectView.frame = CGRectMake(0, 100, 375, 200);
// 3.添加到view当中
[self.view addSubview:effectView];
//添加显示文本
UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
label.text = @"模糊效果";
label.font = [UIFont systemFontOfSize:40];
label.textAlignment = NSTextAlignmentCenter;
//添加模糊效果的子view
// 1.创建出子模糊view
UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
// 2.设置子模糊view的尺寸
subEffectView.frame = effectView.bounds;
// 3.将子模糊view添加到effectView的contentView上才能显示
[effectView.contentView addSubview:subEffectView];
// 4.添加要显示的view来达到特殊效果
[subEffectView.contentView addSubview:label];