iOS development-image Gaussian blur effect

Source: Internet
Author: User

Http://www.cnblogs.com/xiaofeixiang/p/5129074.html

iOS development sometimes need to blur the picture, or by clicking the drop-down method, to remove the blur, everything is for the application more popular with users, after iOS7 translucent blur effect to get a large range of use of the relatively large, and now can see a lot of applications to use the image blur effect, There are three ways to achieve the Gaussian blur effect on a picture, Coreimage,Gpuimage (third party Open source class library) and Vimage. Gpuimage not used much, this article is about two ways of core image and Vimage.

Core Image

Let's take a look at the effect of the implementation before starting the code:

After iOS5.0, the API for the Api,core image of core image is placed in the Coreimage.framework library, and the core image provides a large number of filters (filter) on the iOS and OS X platforms, and the OS There are more than 120 kinds of filter on X, and more than 90 on iOS. First we extend the UIImage and add the class method:

12345678910111213141516 +(UIImage *)coreBlurImage:(UIImage *)image           withBlurNumber:(CGFloat)blur {    //博客园-FlyElephant    CIContext *context = [CIContext contextWithOptions:nil];    CIImage  *inputImage=[CIImage imageWithCGImage:image.CGImage];    //设置filter    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];    [filter setValue:inputImage forKey:kCIInputImageKey];    [filter setValue:@(blur) forKey: @"inputRadius"];    //模糊图片    CIImage *result=[filter valueForKey:kCIOutputImageKey];    CGImageRef outImage=[context createCGImage:result fromRect:[result extent]];    UIImage *blurImage=[UIImage imageWithCGImage:outImage];    CGImageRelease(outImage);    returnblurImage;}

Where the filtered option is set to Gaussian blur:

  

Vimage Way

Vimage belongs to Accelerate.framework, need to import accelerate under the accelerate header file, accelerate is mainly used to do digital signal processing, image processing related vector, matrix Operations Library. Images can be considered to be composed of vectors or matrix data, accelerate, since the provision of efficient mathematical operations API, natural can be convenient for us to do a variety of image processing , fuzzy algorithm using vimageboxconvolve_argb8888 this function.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 +(UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur {    if(blur < 0.f || blur > 1.f) {        blur = 0.5f;    }    intboxSize = (int)(blur * 40);    boxSize = boxSize - (boxSize % 2) + 1;        CGImageRef img = image.CGImage;        vImage_Buffer inBuffer, outBuffer;    vImage_Error error;        void*pixelBuffer;    //从CGImage中获取数据    CGDataProviderRef inProvider = CGImageGetDataProvider(img);    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);    //设置从CGImage获取对象的属性    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);        returnreturnImage;}

Image Blur Call:

12345 self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 300, SCREENWIDTH, 100)];self.imageView.contentMode=UIViewContentModeScaleAspectFill;self.imageView.image=[UIImage boxblurImage:self.image withBlurNumber:0.5];self.imageView.clipsToBounds=YES;[self.view addSubview:self.imageView];

Suggestions on the choice of two ways

Effect: The first core image setting blur will create white edges around, vimage use no problem;

Performance: Image obfuscation is a complex calculation, most of the blurred image selection is vimage, the best performance (not personally tested, interested can test themselves)

Project Address: Https://github.com/SmallElephant/iOS-UIImageBoxBlur

Reference: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/ Index.html#//apple_ref/doc/filter/ci/cigaussianblur

iOS development-image Gaussian blur 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.