实现图片模糊(背景虚化)

图片模糊效果

背景虚化(模糊)的效果越来越常用,那么如何使用代码来实现呢?在上篇文章中,我们讨论了关于 CoreImage 的知识,

理所当然的,首先尝试使用 CoreImage 解决问题,从上次打印出来的所有支持的滤镜中,在 127种滤镜中,存在 Blur 关键字的,只有CIGaussianBlur(高斯模糊),,而这种模糊的致命缺陷是,会出现白边.(懂美术的同学可能知道,这是位图和矢量图的原因)
高斯模糊代码

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    // create gaussian blur filter
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:10.0] forKey:@"inputRadius"];
    // blur image
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    self.mainImageView.image = image;

实现图片模糊(背景虚化)_第1张图片

那么依照我现在的知识,使用 CoreImage 就没法解决问题了…
通过查找参考资料,发现 iOS5.0中新增了vImage API可以使用,它属于Accelerate.Framework,所以如果你要使用它要在工程中加入这个Framework。模糊算法使用的是vImageBoxConvolve_ARGB8888这个函数

-(UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if (blur < 0.f || blur > 1.f) {
        blur = 0.5f;
    }
    int boxSize = (int)(blur * 100);
    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(@"error from convolution %ld", 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];

    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);

    free(pixelBuffer);
    CFRelease(inBitmapData);

    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);

    return returnImage;
}  

最终效果:
实现图片模糊(背景虚化)_第2张图片

你可能感兴趣的:(【只言片语IOS】,效果,代码)