Friday, 21 June 2013

UIImage - Make Image Blur

In iOs 7 there is grate use of blur image. Its look awesome .. Here is function to make any image blur. In that original image is blur start with 0.0 and increase it unto 1.0 as requirement.

Example :

- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if ((blur < 0.0f) || (blur > 1.0f)) {
        blur = 0.5f;
    }
   
    int boxSize1 = (int)(blur * 100);
    boxSize1 -= (boxSize1 % 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));
       
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
   
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,
                                       0, 0, boxSize1, boxSize1, 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,
                                             CGImageGetBitmapInfo(image.CGImage));
   
    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;
}



Hope this will useful in your code…. if any useful code for same please share it with us…..Thank You…:)

No comments:

Post a Comment