博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - 毛玻璃效果封装
阅读量:6487 次
发布时间:2019-06-24

本文共 3370 字,大约阅读时间需要 11 分钟。

 
1 #import 
2 #import
3 4 @interface UIImage (TY_ImageEditP) 5 /** 6 * 对图片进行模糊 7 * 8 * @param image 要处理图片 9 * @param blur 模糊系数 (0.0-1.0)10 *11 * @return 处理后的图片12 */13 + (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur;14 15 @end
 

 

1 #import "UIImage+TY_ImageEditP.h" 2  3 @implementation UIImage (TY_ImageEditP) 4 + (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur { 5     if (!image) { 6         return nil; 7     } 8     if ((blur < 0.0f) || (blur > 1.0f)) { 9         blur = 0.5f;10     }11     12     int boxSize = (int)(blur * 200);13     boxSize -= (boxSize % 2) + 1;14     15     CGImageRef img = image.CGImage;16     17     vImage_Buffer inBuffer, outBuffer;18     vImage_Error error;19     void *pixelBuffer;20     21     CGDataProviderRef inProvider = CGImageGetDataProvider(img);22     CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);23     24     inBuffer.width = CGImageGetWidth(img);25     inBuffer.height = CGImageGetHeight(img);26     inBuffer.rowBytes = CGImageGetBytesPerRow(img);27     inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);28     29     pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));30     31     outBuffer.data = pixelBuffer;32     outBuffer.width = CGImageGetWidth(img);33     outBuffer.height = CGImageGetHeight(img);34     outBuffer.rowBytes = CGImageGetBytesPerRow(img);35     36     error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,37                                        0, 0, boxSize, boxSize, NULL,38                                        kvImageEdgeExtend);39     40     41     if (error) {42         NSLog(@"error from convolution %ld", error);43     }44     45     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();46     CGContextRef ctx = CGBitmapContextCreate(47                                              outBuffer.data,48                                              outBuffer.width,49                                              outBuffer.height,50                                              8,51                                              outBuffer.rowBytes,52                                              colorSpace,53                                              CGImageGetBitmapInfo(image.CGImage));54     55     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);56     UIImage *returnImage = [UIImage imageWithCGImage:imageRef];57     58     //clean up59     CGContextRelease(ctx);60     CGColorSpaceRelease(colorSpace);61     62     free(pixelBuffer);63     CFRelease(inBitmapData);64     65     CGColorSpaceRelease(colorSpace);66     CGImageRelease(imageRef);67     68     return returnImage;69 }70 71 @end

 

例子~:

1  __weak typeof(self) weakSelf = self;2                 [self.myImageView sd_setImageWithURL:[NSURL URLWithString:[ZLModel checkImageUrlNsstring:myModel.address withHttp:TY_IMAGEURL]] placeholderImage:[UIImage imageNamed:consultDefault] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {3                     //毛玻璃效果处理4                     UIImage *imageN = [UIImage blurryImage:image withBlurLevel:0.2];5                     weakSelf.imageN = imageN;6                     self.myImageView.image = self.imageN;7 8                 }];

 

转载于:https://www.cnblogs.com/henusyj-1314/p/9523668.html

你可能感兴趣的文章
sqlserver 批量删除存储过程(转)
查看>>
自建型呼叫中心
查看>>
Inno setup中定制安装路径
查看>>
要懂得对你的老板好一点!
查看>>
visio如何让动态连接线的单箭头变成双箭头?
查看>>
poj 1273 Drainage Ditches 网络流最大流基础
查看>>
Bash: how to check if a process id (PID) exists
查看>>
Mirantis Fuel fundations
查看>>
启动Tomcat一闪而过——分析及解决过程
查看>>
Android intent action大全
查看>>
使用 Flash Builder 的 Apple iOS 开发过程
查看>>
RabbitMq_05_Topics
查看>>
redis.conf
查看>>
SCALA中的函数式编程
查看>>
将List<int> 转换为用逗号连接为字符串
查看>>
C/C++中extern关键字详解
查看>>
Eclipse 最有用的快捷键
查看>>
K & DN 的前世今生(微软开源命名变革)
查看>>
--@angularJS--angular与BootStrap3的应用
查看>>
Flask服务入门案例
查看>>