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毛玻璃效果的实现方法的更多相关文章

  1. iOS 毛玻璃效果的实现方法

    iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...

  2. CSS3中毛玻璃效果的使用方法

    今天在使用icloud的时候看到苹果icloud官网的毛玻璃效果非常赞,仔细研究了一下它的实现方式,是使用js配合background-image: -webkit-canvas的形式绘制出的毛玻璃背 ...

  3. iOS - 毛玻璃效果封装

    #import <UIKit/UIKit.h> #import <Accelerate/Accelerate.h> @interface UIImage (TY_ImageEd ...

  4. ios毛玻璃效果

    方法一:支持所有ios系统版本: - (void)setupBlurView { UIImageView *darkView = [[UIImageView alloc] init]; darkVie ...

  5. ios 毛玻璃效果

    推荐第三方库: https://github.com/nicklockwood/FXBlurView FXBlurView*Fx=[[FXBlurView alloc]initWithFrame:CG ...

  6. view添加毛玻璃效果两种方法

    第一种方法: UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectV ...

  7. 解决css3毛玻璃效果(blur)有白边问题

    做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: <body> <div class="login-wrap"> <div class= ...

  8. CSS 奇思妙想 | 全兼容的毛玻璃效果

    通过本文,你能了解到 最基本的使用 CSS backdrop-filter 实现磨砂玻璃(毛玻璃)的效果 在至今不兼容 backdrop-filter 的 firefox 浏览器,如何利用一些技巧性的 ...

  9. iOS开发小技巧--实现毛玻璃效果的方法

    一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...

随机推荐

  1. ros 学习 array 的添加

    array的添加,使用 arry[i]赋值时会出现段错误,需要使用array.push_back添加到数据中,在ros中array数组是以vector方式存储的. 例如: 包含数组的msg定义为: h ...

  2. 【Redis】Redis 主从模式搭建

    主从模式介绍 Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或者级联结构,Redis主从复制可以根据 ...

  3. 【DataBase】Hsqldb的简单使用

    介绍 HSQLDB是一个开放源代码的JAVA数据库,其具有标准的SQL语法和JAVA接口,它可以自由使用和分发,非常简洁和快速的.具有Server模式,每个程序需要不同的命令来运行. HyperSQL ...

  4. 【设计】IOT设备控制页面设计

    https://www.uishe.cn/10803.html https://huaban.com/pins/1012512760/ https://huaban.com/pins/10878772 ...

  5. angular2 select 联动

    界面操作触发大分类id改变,根据id获取二级分类的数据进行绑定显示. html: <div style="overflow: hidden;float: left;padding-le ...

  6. 内存运行PE文件

    内存中运行文件 拿exe并在HxD或010中打开 - cntrl+a copy as C 粘贴到encrypt.cpp 编译并运行encrypt.cpp - 创建shellcode.txt 从shel ...

  7. CA机构介绍(Certificate Authority 域名SSL证书颁发机构)

    SSL证书机构即CA机构的全称为Certificate Authority证书认证中心,只有通过WebTrust国际安全审计认证,根证书才能预装到主流浏览器,成为全球可信的ssl证书颁发机构. HTT ...

  8. 了解 Selenium 定位方式

    ※元素定位的重要性:在于查找元素 And 执行元素 定位元素的三种方法 1.定位单个元素:在定位单个元素时,selenium-webdriver 提示了如下一些方法对元素进行定位.在这些定位方式中,优 ...

  9. C++STL位标志、智能指针与异常处理

    参考<21天学通C++>第25章节,对STL位标志进行介绍.就是当需要不是像char int long 等整个字节数的数据表示形式,而是使用二进制位表示的时候,通常使用这里讲到的位标志.从 ...

  10. 《构建之法》——GitHub和Visual Studio的基础使用

    git地址 https://github.com/microwangwei git用户名 microwangwei 学号后五位 62214 博客地址 https://www.cnblogs.com/w ...