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. 虚拟机的Vmtools

    安装了虚拟机之后,文件共享不方便,安装VMTools可以在windows上直接拖文件到linux上. 安装方法: 1.进入linux把CD弹出 2.打开虚拟机之后 3.下载完成可以在linux的CD设 ...

  2. 报错:The specified datastore driver ("com.mysql.jdbc.Driver") was not found in the CLASSPATH. Please check your CLASSPATH specification, and the name of the driver.

    报错背景: CDH中集成hive插件,启动报错. 报错现象: [main]: Metastore Thrift Server threw an exception... javax.jdo.JDOFa ...

  3. pip 使用国内源安装第三方库

    pip3 install django -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com​

  4. 【物联网】传感器+wifi传输+回复+显示

    https://www.jianshu.com/p/cb0274d612b5 https://timgsa.baidu.com/timg?image&quality=80&size=b ...

  5. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  6. hadoop(四)MapReduce

    如果将 Hadoop 比做一头大象,那么 MapReduce 就是那头大象的电脑.MapReduce 是 Hadoop 核心编程模型.在 Hadoop 中,数据处理核心就是 MapReduce 程序设 ...

  7. python 爬虫实例(二)

    环境: OS:Window10 python:3.7 描述 打开下面的网址,之后抓取其中的图片 https://music.163.com/#/artist/album?id=101988&l ...

  8. TypeScript之路----探索接口(interface)的奥秘

    TypeScript定义接口 要想掌握typescript的知识,接口是其必经之路.很多东西都需要接触到接口,接口除了对类的一部分行为进行抽象以外,也常用于对对象的形状进行描述.接下来我们就一起来学习 ...

  9. JVM(四) 垃圾回收

    1. 堆内存结构 Java堆从GC的角度可以细分为:新生代(Eden区.From Survivor区和To Survivor区)和老年代. 1.1 新生代 新生代是用来存放新生的对象.一般占据堆的1/ ...

  10. hdoj4099(字典树+高精度)

    题目链接:https://vjudge.net/problem/HDU-4099 题意:给T组询问,每个询问为一个字符串(长度<=40),求以该字符串为开始的fibonacci数列的第一个元素的 ...