iOS 让图片变模糊

#import <Accelerate/Accelerate.h>数组

1.初始化图片ide

    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.让图片变模糊的方法ip

-(UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blurit

{ast

    if (blur <0.f || blur > 1.f)import

        {map

            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;

}

相关文章
相关标签/搜索