引入文件
#import <Accelerate/Accelerate.h>
#import <float.h>
//磨砂效果
- (UIImage *)blurryImageWithBlurLevel:(CGFloat)blur{
intboxSize = (int)(blur *40);
boxSize = boxSize - (boxSize %2) +1;
CGImageRefimg =self.CGImage;
vImage_BufferinBuffer, outBuffer;
vImage_Error error;
void*pixelBuffer;
//create vImage_Buffer with data from CGImageRef
CGDataProviderRefinProvider =CGImageGetDataProvider(img);
CFDataRefinBitmapData =CGDataProviderCopyData(inProvider);
inBuffer.width=CGImageGetWidth(img);
inBuffer.height=CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);
inBuffer.data= (void*)CFDataGetBytePtr(inBitmapData);
//create vImage_Buffer for output
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);
//perform convolution
error =vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer,NULL,0,0, boxSize, boxSize,NULL,kvImageEdgeExtend)
?:vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer,NULL,0,0, boxSize, boxSize,NULL,kvImageEdgeExtend)
?: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,
(CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage*returnImage = [UIImageimageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
//free(pixelBuffer2);
CFRelease(inBitmapData);
CGImageRelease(imageRef);
returnreturnImage;
}