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
iOS毛玻璃效果的实现方法的更多相关文章
- iOS 毛玻璃效果的实现方法
iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...
- CSS3中毛玻璃效果的使用方法
今天在使用icloud的时候看到苹果icloud官网的毛玻璃效果非常赞,仔细研究了一下它的实现方式,是使用js配合background-image: -webkit-canvas的形式绘制出的毛玻璃背 ...
- iOS - 毛玻璃效果封装
#import <UIKit/UIKit.h> #import <Accelerate/Accelerate.h> @interface UIImage (TY_ImageEd ...
- ios毛玻璃效果
方法一:支持所有ios系统版本: - (void)setupBlurView { UIImageView *darkView = [[UIImageView alloc] init]; darkVie ...
- ios 毛玻璃效果
推荐第三方库: https://github.com/nicklockwood/FXBlurView FXBlurView*Fx=[[FXBlurView alloc]initWithFrame:CG ...
- view添加毛玻璃效果两种方法
第一种方法: UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectV ...
- 解决css3毛玻璃效果(blur)有白边问题
做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: <body> <div class="login-wrap"> <div class= ...
- CSS 奇思妙想 | 全兼容的毛玻璃效果
通过本文,你能了解到 最基本的使用 CSS backdrop-filter 实现磨砂玻璃(毛玻璃)的效果 在至今不兼容 backdrop-filter 的 firefox 浏览器,如何利用一些技巧性的 ...
- iOS开发小技巧--实现毛玻璃效果的方法
一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...
随机推荐
- python中的修饰符@的作用
1.一层修饰符 1)简单版,编译即实现 在一个函数上面添加修饰符 @另一个函数名 的作用是将这个修饰符下面的函数作为该修饰符函数的参数传入,作用可以有比如你想要在函数前面添加记录时间的代码,这样每个函 ...
- visual studio code跳转到定义处插件
visual studio code 中使用跳转到定义处的插件 https://marketplace.visualstudio.com/items?itemName=Shan.code-settin ...
- Qt编写自定义控件61-通用移动
一.前言 通用移动类,目标就是为了实现放入任意的控件以后,支持鼠标拖动,在容器中或者父类中拖动,这个应用场景非常多,比如在地图上放置的设备,需要用户自行按下拖动到指定的合适的位置,然后保存设备的位置坐 ...
- PHP IE9 AJAX success 返回 undefined 问题解决
jquery的AJAX返回结果为undefined,并且有“由于出现错误c00ce56e”的错误提示.这个问题是由于IE9不能解析其他编码而产生的.解决这个问题之需要按照W3C规范,声明一下编码为ut ...
- 让Chrome浏览器抓包接口数据秒变 python 代码
简介 uncurl是一个库,允许您将curl请求转换为使用requests 的python代码.由于Chrome网络检查器具有的“copy as cURL”,因此该工具对于用python重新创建浏览器 ...
- websehll的使用和预防措施
(1).webshell概念 webshell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将asp或php后 ...
- 05点睛Spring MVC 4.1-服务器端推送
转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...
- AtCoder Beginner Contest 147 E. Balanced Path
思路: dp,使用了bitset优化. 实现: #include <bits/stdc++.h> using namespace std; ; const int INF = 0x3f3f ...
- axios 使用入门
[Vue 牛刀小试]:第十五章 - 传统开发模式下的 axios 使用入门 一.前言# 在没有接触 React.Angular.Vue 这类 MVVM 的前端框架之前,无法抛弃 Jquery 的重 ...
- js18位身份证验证(非原创)
原文链接 function check_id(value) { var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];/ ...