iOS Development Quest-Gaussian blur & frosted glass effects

Source: Internet
Author: User

iOS development sometimes need to blur the picture, to achieve a specific effect to obtain a better user experience, after iOS7 translucent blur effect to get a large range of use of the larger, now also can see a lot of applications to use the image blur effect, can be through the Gaussian blur and the effect of the glass to achieve the image blur effect.

Left Glass right Blur

Gaussian Blur effect

1. Coreimage:
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.

+ (UIImage *) Coreblurimage: (UIImage *) image withblurnumber: (CGFloat) Blur {     Cicontext *context = [Cicontext Contextwithoptions:nil];     Ciimage *inputimage= [Ciimage imagewithcgimage:image. Cgimage];     Set Filter    cifilter *filter = [Cifilter filterwithname:@ "Cigaussianblur"];     [Filter Setvalue:inputimage Forkey:kciinputimagekey]; [Filter setvalue:@ (Blur) Forkey: @ "Inputradius"];     Blur picture    ciimage *result=[filter Valueforkey:kcioutputimagekey];     Cgimageref Outimage=[context Createcgimage:result fromrect:[result extent]];           UIImage *blurimage=[uiimage Imagewithcgimage:outimage];               Cgimagerelease (outimage);     return blurimage;}

2. Vimage
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.

 

+ (UIImage *) Boxblurimage: (UIImage *) image withblurnumber: (CGFloat) Blur {if (Blur < 0.F | | Blur > 1.F) {      Blur = 0.5f;     } int boxsize = (int) (blur * 40);      Boxsize = boxsize-(boxsize% 2) + 1; Cgimageref img = image.      Cgimage;      Vimage_buffer Inbuffer, Outbuffer;      Vimage_error Error;      void *pixelbuffer;     Get data from cgimage cgdataproviderref inprovider = Cgimagegetdataprovider (IMG);      Cfdataref inbitmapdata = Cgdataprovidercopydata (Inprovider);     Sets the property Inbuffer.width = Cgimagegetwidth (img) from Cgimage to get the object;      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;}

Method invocation

Uiimageview  *imageview=[[uiimageview alloc]initwithframe:cgrectmake (0, ScreenWidth, 100)]; Imageview.contentmode=uiviewcontentmodescaleaspectfill;imageview.image=[uiimage BoxblurImage:image withblurnumber:0.5]; Imageview.clipstobounds=yes; [Self.view Addsubview:imageview];

3.GPUImage

Gpuimage is an open-source library that uses the device's GPU to process images in real time and add various filter effects to images.
You can add filter effects to your camera in real time, and many apps support this real-time filter.
Link Address: Https://github.com/BradLarson/GPUImage

Difference:
Effect: The first core image setting blur will create white edges around, vimage use no problem;
Performance: Image fuzzy processing is a complex calculation, most of the blurred image selection is vimage, the best performance

Project Address: Https://github.com/524429264/iOS-UIImageBoxBlur
Reference: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/ Index.html#//apple_ref/doc/filter/ci/cigaussianblur

frosted Glass effect

1, Apple after iOS7.0, a lot of system interface has used the glass effect, increase the appearance of the interface, such as Notification Center interface, in fact, before iOS7.0 (including) there is a system of class can achieve the effect of glass, is Uitoolbar this class

iOS7.0 the style of the frosted Glass (enumeration) Uibarstyledefault          = 0,uibarstyleblack            = 1,uibarstyleblackopaque      = 1,//Deprecated. Use Uibarstyleblackuibarstyleblacktranslucent = 2,//Deprecated. Use Uibarstyleblack and set the translucent property to Yesuiimageview *bgimgview = [[Uiimageview alloc] Initwithframe:sel F.view.bounds];bgimgview.image = [UIImage imagenamed:@ "huoying4.jpg"]; [Self.view Addsubview:bgimgview]; Uitoolbar *toolbar = [[Uitoolbar alloc] Initwithframe:cgrectmake (0, 0, bgimgview.frame.size.width*0.5, BgImgView.frame.size.height)];toolbar.barstyle = uibarstyleblacktranslucent; [Bgimgview Addsubview:toolbar];

2, after the iOS8.0, Apple added a class Uivisualeffectview, through this class to achieve the effect of the glass with the above uitoolbar, and the efficiency is very high, the use is very simple, a few lines of code to get it done. Uivisualeffectview is an abstract class that cannot be used directly, and needs to be implemented by the following three subclasses (Uiblureffect, uivisualeffevt, Uivisualeffectview);

Subclass Uiblureffect has only one class method, which is used to quickly create a frosted glass effect, the parameter is an enumeration to set the style of the frosted glass, and Uivisualeffectview has two more properties and two construction methods. Used to quickly add the created frosted glass to the Uivisualeffectview.

Special Note: This class is iOS8.0 after the application, so if the project to be compatible with iOS7.0, still need to consider the other two methods.

Implementation code:

The same is to quickly instantiate the Uiblureffect and set the style of the frosted glass, and then through the construction method of Uivisualeffectview to add the Uiblureffect instance, and finally set the frame or by adding a constraint, Add the Effectview to the view control that will implement the effect of the frosted glass, as in the above.

iOS8.0 Style (enumeration) Uiblureffectstyleextralight, Uiblureffectstylelight, uiblureffectstyledark uiblureffect *effect = [ Uiblureffect Effectwithstyle:uiblureffectstyledark]; Uivisualeffectview *effectview = [[Uivisualeffectview alloc] initwitheffect:effect]; Effectview.frame = CGRectMake (0, 0, bgimgview.frame.size.width*0.5, bgImgView.frame.size.height); [Bgimgview Addsubview:effectview];

Project Address: Https://github.com/524429264/EffectViewDemo

Source: Http://www.jianshu.com/p/6dd0eab888a6

iOS Development Quest-Gaussian blur & frosted glass effects

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.