标签:
#import <Accelerate/Accelerate.h>
1.初始化图片
UIImageView *iv = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[iv setImage:[self blurryImage:[UIImage imageNamed:@"welcome_2.jpg"] withBlurLevel:0.6]];
[self.view addSubview:iv];
2.让图片变模糊的方法
-(UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur
{
if (blur <0.f || blur > 1.f)
{
blur = 0.5f;
}
//判断曝光度
int boxSize = (int)(blur * 100);//放大100 小数点后面2位有效
boxSize = boxSize - (boxSize % 2) + 1;//如果是偶数 变奇数
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));//创建一个空间
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(@"%zd",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];
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inbitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
标签:
原文地址:http://www.cnblogs.com/biggestfish/p/5040013.html