App design often uses some blur effect. iOS has now provided some fuzzy APIs that can make it easy for us to use. One is to use the core Image, and the other is to use the Vimage API in Accelerate.framework.
Blur using the core image
The Core image was applied early in the Mac system, and later the framework was applied to iOS, but it wasn't until iOS6.0 began to support Blur. This API is easy to call and concise.
[CPP]View Plaincopy
- -(UIImage *) Blurryimage: (UIImage *) image
- Withblurlevel: (CGFloat) Blur {
- Ciimage *inputimage = [Ciimage imagewithcgimage:image. Cgimage];
- Cifilter *filter = [Cifilter filterwithname:@ "Cigaussianblur"
- Keysandvalues:kciinputimagekey, Inputimage,
- @ "Inputradius" @ (Blur),
- NIL];
- Ciimage *outputimage = filter.outputimage;
- Cgimageref outimage = [Self.context createcgimage:outputimage
- Fromrect:[outputimage extent]];
- return [UIImage Imagewithcgimage:outimage];
- }
Blur using the Vimage API
The new Vimage API is available in iOS5.0, which belongs to Accelerate.framework, so if you want to use it, you need to add the Framework to the project. The fuzzy algorithm uses the vimageboxconvolve_argb8888 function.
[CPP]View Plaincopy
- -(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;
- }
iOS Gaussian blur processing