iOS 毛玻璃效果的实现方法
iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效果达到图片模糊效果。
高斯模糊效果
1.** CoreImage: **
iOS5.0之后就出现了Core Image的API,Core Image的API被放在CoreImage.framework库中, 在iOS和OS X平台上,Core Image都提供了大量的滤镜(Filter),在OS X上有120多种Filter,而在iOS上也有90多。
+(UIImage *)coreBlurImage:(UIImage *)image withBlurNumber:(CGFloat)blur
{ CIContext *context = [CIContext contextWithOptions:nil]; CIImage *inputImage= [CIImage imageWithCGImage:image.CGImage]; //设置filter CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; [filter setValue:inputImage forKey:kCIInputImageKey]; [filter setValue:@(blur) forKey: @"inputRadius"]; //模糊图片 CIImage *result=[filter valueForKey:kCIOutputImageKey]; CGImageRef outImage=[context createCGImage:result fromRect:[result extent]]; UIImage *blurImage=[UIImage imageWithCGImage:outImage]; CGImageRelease(outImage); return blurImage; }
2.** vImage **
vImage属于Accelerate.Framework,需要导入 Accelerate下的 Accelerate头文件, Accelerate主要是用来做数字信号处理、图像处理相关的向量、矩阵运算的库。图像可以认为是由向量或者矩阵数据构成的,Accelerate里既然提供了高效的数学运算API,自然就能方便我们对图像做各种各样的处理 ,模糊算法使用的是vImageBoxConvolve_ARGB8888这个函数。
+(UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur{ if (blur < 0.f || blur > 1.f) { blur = 0.5f; } int boxSize = (int)(blur * 40); boxSize = boxSize - (boxSize % 2) + 1; CGImageRef img = image.CGImage; vImage_Buffer inBuffer, outBuffer; vImage_Error error; void *pixelBuffer; //从CGImage中获取数据 CGDataProviderRef inProvider = CGImageGetDataProvider(img); CFDataRef inBitmapData = CGDataProviderCopyData(inProvider); //设置从CGImage获取对象的属性 inBuffer.width = CGImageGetWidth(img); inBuffer.height = CGImageGetHeight(img); inBuffer.rowBytes = CGImageGetBytesPerRow(img); inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData); pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); if(pixelBuffer == NULL) NSLog(@"No pixelbuffer"); outBuffer.data = pixelBuffer; outBuffer.width = CGImageGetWidth(img); outBuffer.height = CGImageGetHeight(img); outBuffer.rowBytes = CGImageGetBytesPerRow(img); error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); if (error) { NSLog(@"error from convolution %ld", error); } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate( outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, kCGImageAlphaNoneSkipLast); CGImageRef imageRef = CGBitmapContextCreateImage (ctx); UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; //clean up CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); free(pixelBuffer); CFRelease(inBitmapData); CGColorSpaceRelease(colorSpace); CGImageRelease(imageRef); return returnImage; }
方法调用
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 300, SCREENWIDTH, 100)]; imageView.contentMode=UIViewContentModeScaleAspectFill; imageView.image=[UIImage boxblurImage:image withBlurNumber:0.5]; imageView.clipsToBounds=YES; [self.view addSubview:imageView];
3.GPUImage
GPUImage是用设备的GPU来实时处理图片,给图片加各种滤镜效果的一个开源库。
可以实时地给照相机加上滤镜效果,很多App都支持这种实时滤镜。
链接地址:https://github.com/BradLarson/GPUImage
区别:
效果:第一种Core Image设置模糊之后会在周围产生白边,vImage使用不存在任何问题;
性能:图像模糊处理属于复杂的计算,大部分图片模糊选择的是vImage,性能最佳
项目地址: https://github.com/524429264/iOS-UIImageBoxBlur
参考资料:https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur
毛玻璃效果
- 苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如通知中心界面;其实在iOS7.0(包括)之前还是有系统的类可以实现毛玻璃效果的, 就是 UIToolbar这个类
iOS7.0
毛玻璃的样式(枚举)
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];bgImgView.image = [UIImage imageNamed:@"huoying4.jpg"]; [self.view addSubview:bgImgView]; > UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, bgImgView.frame.size.width*0.5, bgImgView.frame.size.height)]; toolbar.barStyle = UIBarStyleBlackTranslucent; [bgImgView addSubview:toolbar];
- 在iOS8.0之后,苹果新增了一个类UIVisualEffectView,通过这个类来实现毛玻璃效果与上面的UIToolbar一样,而且效率也非常之高,使用也是非常简单,几行代码搞定. UIVisualEffectView是一个抽象类,不能直接使用,需通过它下面的三个子类来实现(UIBlurEffect, UIVisualEffevt, UIVisualEffectView);
子类UIBlurEffect只有一个类方法,用来快速创建一个毛玻璃效果,参数是一个枚举,用来设置毛玻璃的样式,而UIVisualEffectView则多了两个属性和两个构造方法,用来快速将创建的毛玻璃添加到这个UIVisualEffectView上.
特别注意: 这个类是iOS8.0之后才适用, 所以如果项目要兼容iOS7.0的话, 还是要考虑其它的两种方法了.
** 实现代码:**
同样是先快速的实例化UIBlurEffect并设置毛玻璃的样式,然后再通过UIVisualEffectView的构造方法将UIBlurEffect的实例添加上去, 最后设置frame或者是通过添加约束, 将effectView添加到要实现了毛玻璃的效果的view控件上,效果图和上面的一样.
iOS8.0毛玻璃的样式(枚举)UIBlurEffectStyleExtraLight,UIBlurEffectStyleLight,UIBlurEffectStyleDark>UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect]; effectView.frame = CGRectMake(0, 0, bgImgView.frame.size.width*0.5, bgImgView.frame.size.height); [bgImgView addSubview:effectView];
项目地址:https://github.com/524429264/EffectViewDemo
转载于:https://www.cnblogs.com/Free-Thinker/p/11347363.html
iOS 毛玻璃效果的实现方法相关推荐
- android 仿ios毛玻璃,类 iOS 毛玻璃效果控件 BlurView
软件介绍 BlurView 是Android下类似 iOS 毛玻璃效果控件. 使用: android:id="@+id/blurView" android:layout_width ...
- iOS毛玻璃效果(不需要任何第三方了)
分享两种毛玻璃效果设置的方法,不需要任何三方东东,先看效果: 原图: 方法一(iOS8系统方法): 方法二: 下面是示例代码: [objc] view plain copy #import " ...
- iOS毛玻璃效果(第一次写~有点紧张喔)
iOS7以后,苹果公司提供了一种实时毛玻璃的渲染效果,苹果提供了一些方法让我们可以在UIImage上增加毛玻璃效果,但是,只是静态的效果,不是实时的,要是想要实时的效果,那该怎么办呢? 上代码:(很简 ...
- css毛玻璃效果白边_CSS3毛玻璃效果(blur)有白边问题的解决方法
做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: var w = window.innerWidth || document.documentElement.clientWidth || ...
- 用css使图片产生毛玻璃效果
毛玻璃的图片效果,使得其显示更加唯美,用户体验更佳,所以在手机端电脑端应用甚广 比如ios系统: 所以现在我们就来看看毛玻璃效果的实现方法吧: 首先这是html的主要内容: <div class ...
- 图片处理 毛玻璃效果
1 // iOS 毛玻璃效果: 2 UIVisualEffectView *bgView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEff ...
- android动态毛玻璃,Android模糊处理实现图片毛玻璃效果
本文实例讲解了Android 虚化图片.模糊图片.图片毛玻璃效果的实现方法,具体内容如下 效果如图: 在Android可以用RenderScript方便的实现这个方法: private void bl ...
- css filter blur 白边,解决css3毛玻璃效果(blur)有白边问题
做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: var w = window.innerWidth || document.documentElement.clientWidth || ...
- css filter blur 白边,如何解决CSS3毛玻璃效果blur有白边的问题
如何解决CSS3毛玻璃效果blur有白边的问题 发布时间:2021-06-29 11:59:54 来源:亿速云 阅读:81 作者:小新 这篇文章给大家分享的是有关如何解决CSS3毛玻璃效果blur有白 ...
最新文章
- Java学习总结:41(文件操作类:File)
- Python图像处理:图像腐蚀与图像膨胀
- deepin更新失败_检查更新失败
- 《JavaScript面向对象精要》——1.8 原始封装类型
- Kubernetes——自动扩展容器!假设你突然需要增加你的应用;你只需要告诉deployment一个新的 pod 副本总数即可...
- PyQt v4 - Python Bindings for Qt v4 | Документация
- 实时检索系统Zoie实现分析
- ACMMM 2021-多模态宝藏!京东梅涛团队重磅开源第一个适用于多个任务的多模态代码库x-modaler...
- 随机森林(独立思考的重要性)
- 【js高级程序设计】迭代器
- 20151221jquery学习笔记--验证插件
- 20种PLC对应Modbus地址表
- 增强学习训练AI玩游戏
- 凯文·凯利写给年轻人的99条人生建议(99 Additional Bits of Unsolicited Advice)
- 19上海网络赛 Light bulbs (差分)
- python输入一个三位数、输出它的逆序数_从键盘任意输入一个3位整数
- Kinect Fusion三维重建
- 【MATLAB教程案例53】CNN卷积神经网络的MATLAB编程学习和实现,以手势识别为例进行仿真分析
- CAN总线入门、LIN总线入门文档-蓝凑云下载。 LDFEditor下载 ISO-14229、15765、11898下载与阅读
- 场效应管微变等效电路的计算_微变等效电路如图.ppt
热门文章
- Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)
- 在Linux 上安装WAS7.0
- C# 笔记 .net与C#简单说明
- 来自damon的zencart二次开发教程-2.2登录模块分析
- 软件测试面试必备的一些基础理论概念
- IDEA创建Web Project图解
- python模块介绍-locustio:性能测试工具locustio
- 【linux排错】error while loading shared libraries: xxx.so.x 错误的原因和解决办法
- VBA 的编写与执行
- Debian 安装docker