Blur Images (blur background)

Source: Internet
Author: User

Blur Images (blur background)
Blur Images

Background blur is becoming more and more common. How can we use code to implement it? In the previous article, we discussed the knowledge about CoreImage,

Of course, first try to use CoreImage to solve the problem. From all the supported filters printed last time, among the 127 filters, there is a Blur keyword, onlyCIGaussianBlur(Gaussian Blur), and the fatal flaw of this fuzzy blur is that there will be white edges (Art-savvy people may know that this is the reason for bitmap and vector map)
Gaussian fuzzy code

    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;

So according to my current knowledge, using CoreImage cannot solve the problem...
By searching for references, we found that vImage API is available in iOS5.0, which belongs to Accelerate. Framework. Therefore, if you want to use it, you must add this Framework to the project. The fuzzy algorithm uses the vImageBoxConvolve_ARGB8888 function.

-(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;}  

Final effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.