iOS 图片添加模糊效果

iOS图片模糊效果的两种实现方法:
1.CoreImage的模糊滤镜

UIImage * sourceImage = [UIImage imageNamed:@"imageName"];

CIImage * ciImage    = [[CIImage alloc] initWithImage:sourceImage];

CIFilter * blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];

//将图片输入到滤镜中

[blurFilter setValue:ciImage forKey:kCIInputImageKey];

//设置模糊程度

[blurFilter setValue:@(5) forKey:@"inputRadius"];

NSLog(@"查看blurFilter的属性--- %@",blurFilter.attributes);

//将处理之后的图片输出

CIImage * outCiImage    = [blurFilter valueForKey:kCIOutputImageKey];

CIContext * context      = [CIContext contextWithOptions:nil];

*//获取CGImage句柄*

*CGImageRef outCGImageRef = [context createCGImage:outCiImage fromRect:[outCiImage extent]];*

//获取到最终图片

UIImage * resultImage    = [UIImage imageWithCGImage:outCGImageRef];

//释放句柄

CGImageRelease(outCGImageRef);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * sourceImage.size.height / sourceImage.size.width)];

[imageView setImage:resultImage];

[self.view addSubview:imageView];

2.iOS系统自带的高斯模糊效果(只能在iOS8以上使用)

   UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
   UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
   effectView.alpha = 0.7;
   effectView.frame = bgImg.frame;
   [bgImg addSubview:effectView];

你可能感兴趣的:(iOS 图片添加模糊效果)