iOS Image模糊效果四种方案

最近由于项目需求,需要使用图片模糊效果,根据自己的经验和使用心得整理一下几种使用方法。

先看下整体效果

原始图片:

image

模糊效果:

image

梳理了四种方案:

一:在图片上加mask遮罩图片

这个图片大多数都是UI给的切图,大多数初级人员应该都是这么做的,虽然可以实现效果,不过应该比较low,不应该满足于此,应该去了解一下其他解决方案。这种方式比较简单,我就不多说了。

二:CoreImage的模糊滤镜

CoreImage是苹果用来简化图片处理的框架

直接上代码:

*UIImage * sourceImage = [UIImage imageNamed:@"个人中心-好友动态-富文本详情"];* 

*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];*

三:Accelerate框架实现 模糊效果

此种模糊效果比CoreImage效果更美观

这种方式的效果效率要比CoreImage要高。

这种方式可以查看我的demo github地址https://github.com/EricLihuifeng/UIImageCategory

对图片的质量和尺寸压缩、图片剪切、图片模糊效果处理

刚开始玩git demo应该有不足之处 还望指教!

四: iOS8 苹果提供的 UIVisualEffectView模糊效果

特点:

UIVisualEffectView的模糊效果是即时渲染的

如果在UIVisualEffectView上做一些文本显示的话 需要特殊处理

只能在iOS8以上使用

// 高斯模糊的效果
   UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
   UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
   effectView.alpha = 0.7;
   effectView.frame = bgImg.frame;
   [bgImg addSubview:effectView];

原文链接:https://www.jianshu.com/p/46b1dc3adc21

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容