#import <UIKit/UIKit.h>
#import <Accelerate/Accelerate.h> @interface UIImage (TY_ImageEditP)
/**
* 对图片进行模糊
*
* @param image 要处理图片
* @param blur 模糊系数 (0.0-1.0)
*
* @return 处理后的图片
*/
+ (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur; @end
1 #import "UIImage+TY_ImageEditP.h" @implementation UIImage (TY_ImageEditP)
+ (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
if (!image) {
return nil;
}
if ((blur < 0.0f) || (blur > 1.0f)) {
blur = 0.5f;
} int boxSize = (int)(blur * );
boxSize -= (boxSize % ) + ; CGImageRef img = image.CGImage; vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
void *pixelBuffer; CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider); inBuffer.width = CGImageGetWidth(img);
inBuffer.height = CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);
inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData); pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img); error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,
, , boxSize, boxSize, NULL,
kvImageEdgeExtend); if (error) {
NSLog(@"error from convolution %ld", error);
} CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(
outBuffer.data,
outBuffer.width,
outBuffer.height,
,
outBuffer.rowBytes,
colorSpace,
CGImageGetBitmapInfo(image.CGImage)); 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;
} @end
例子~:
__weak typeof(self) weakSelf = self;
[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) {
//毛玻璃效果处理
UIImage *imageN = [UIImage blurryImage:image withBlurLevel:0.2];
weakSelf.imageN = imageN;
self.myImageView.image = self.imageN; }];