iOS毛玻璃效果的实现方法
ios开发中常常用到的毛玻璃效果实现方法
iOS8以后使用系统里的UIBlurEffect可以实现,UIBlurEffect继承自UIVisualEffect
UIBlurEffectStyle有三个值,UIBlurEffectStyleLight , UIBlurEffectStyleExtraLight , UIBlurEffectStyleDark,可以控制毛玻璃的效果.
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
//必须给effcetView的frame赋值,因为UIVisualEffectView是一个加到UIIamgeView上的子视图.
effectView.frame = _imageView.bounds;
[self.imageView addSubview:effectView];
UIVibrancyEffect也继承自UIVisualEffect类,可以用来设置一些特殊的效果.代码如下
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = _imageView.bounds;
UIVibrancyEffect *viBrancyeffect = [UIVibrancyEffect effectForBlurEffect:effect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:viBrancyeffect];
vibrancyEffectView.frame = CGRectMake(100, 0, 200, 200);
UILabel *lbl = [[UILabel alloc] init];
lbl.text = @"测试Label";
lbl.font = [UIFont systemFontOfSize:25];
lbl.frame = CGRectMake(0, 0, 200, 200);
[vibrancyEffectView.contentView addSubview:lbl];
[effectView.contentView addSubview:vibrancyEffectView];
[self.imageView addSubview:effectView];
但是这种毛玻璃效果不能很好的控制模糊效果,可以alpha属性控制并不完美.
效果如下:

使用系统CoreImage中的滤镜产生毛玻璃效果
原理是给图片添加滤镜,这种方式相比上面更为可控,下面介绍一下系统滤镜中支持的毛玻璃效果
先简要介绍一下系统滤镜CIFilter的使用
CIfilter中有一个专门用于毛玻璃效果的Category : kCICategoryBlur
使用下面的代码可以打印出这个分类下的滤镜
NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBlur];
可以得到结果
** CIBoxBlur,**
** CIDiscBlur,**
** CIGaussianBlur,**
** CIMaskedVariableBlur,**
** CIMedianFilter,**
** CIMotionBlur,**
** CINoiseReduction,**
** CIZoomBlur**
我们使用最常见的高斯模糊 (Gaussian Blur) 来进行举例
NSArray *inputKeys = filter.inputKeys;
可以得到这个滤镜支持两个输入属性,分别是inputImage,inputRadius
其中inputImage指你需要添加滤镜效果的图片,inputRadius指进行高斯模糊的程度
设置属性的方式有两种
一种是直接通过NSDictionary赋值
CIImage *testCIImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"testImg.jpg"].CGImage];
NSDictionary *filterAttDict = @{@"inputImage" : testCIImage
,@"inputRadius" : [NSNumber numberWithDouble:5.0f]};
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" withInputParameters:filterAttDict];
CIImage *outPutCIImage = [filter outputImage];
CIContext *ciContext = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [ciContext createCGImage:outPutCIImage fromRect:outPutCIImage.extent];
UIImage *resImage = [UIImage imageWithCGImage:cgImage];
另一种是通过kvc方法赋值
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
CIImage *testCIImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"testImg.jpg"].CGImage];
[filter setValue:testCIImage forKeyPath:kCIInputImageKey];
[filter setValue:@50 forKeyPath:kCIInputRadiusKey];
CIImage *outPutCIImage = [filter outputImage];
CIContext *ciContext = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [ciContext createCGImage:outPutCIImage fromRect:outPutCIImage.extent];
UIImage *resImage = [UIImage imageWithCGImage:cgImage];
发现一个写的比较详细的博客,可以参考 http://www.jianshu.com/p/d115836ed3fa
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2015-08-13 Android中Parcelable序列化总结
2014-08-13 ndroid网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合
2014-08-13 使用单例模式实现自己的HttpClient工具类
2013-08-13 使用Fragment适应不同屏幕和分辨率
2013-08-13 fragment 小结
2013-08-13 android中listview分页加载数据
2013-08-13 android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)