iOS Gaussian blur processing

Source: Internet
Author: User

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
  1. -(UIImage *) Blurryimage: (UIImage *) image
  2. Withblurlevel: (CGFloat) Blur {
  3. Ciimage *inputimage = [Ciimage imagewithcgimage:image. Cgimage];
  4. Cifilter *filter = [Cifilter filterwithname:@ "Cigaussianblur"
  5. Keysandvalues:kciinputimagekey, Inputimage,
  6. @ "Inputradius" @ (Blur),
  7. NIL];
  8. Ciimage *outputimage = filter.outputimage;
  9. Cgimageref outimage = [Self.context createcgimage:outputimage
  10. Fromrect:[outputimage extent]];
  11. return [UIImage Imagewithcgimage:outimage];
  12. }
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
  1. -(UIImage *) Blurryimage: (UIImage *) image withblurlevel: (CGFloat) Blur {
  2. if (Blur < 0.F | | Blur > 1.F) {
  3. Blur = 0.5f;
  4. }
  5. int boxsize = (int) (blur * 100);
  6. Boxsize = boxsize-(boxsize% 2) + 1;
  7. Cgimageref img = image. Cgimage;
  8. Vimage_buffer Inbuffer, Outbuffer;
  9. Vimage_error Error;
  10. void *pixelbuffer;
  11. Cgdataproviderref inprovider = Cgimagegetdataprovider (IMG);
  12. Cfdataref inbitmapdata = Cgdataprovidercopydata (Inprovider);
  13. Inbuffer.width = Cgimagegetwidth (IMG);
  14. Inbuffer.height = Cgimagegetheight (IMG);
  15. inbuffer.rowbytes = Cgimagegetbytesperrow (IMG);
  16. Inbuffer.data = (void*) cfdatagetbyteptr (inbitmapdata);
  17. Pixelbuffer = malloc (Cgimagegetbytesperrow (IMG) *
  18. Cgimagegetheight (IMG));
  19. if (Pixelbuffer = = NULL)
  20. NSLog (@ "No pixelbuffer");
  21. Outbuffer.data = Pixelbuffer;
  22. Outbuffer.width = Cgimagegetwidth (IMG);
  23. Outbuffer.height = Cgimagegetheight (IMG);
  24. outbuffer.rowbytes = Cgimagegetbytesperrow (IMG);
  25. Error = vimageboxconvolve_argb8888 (&inbuffer,
  26. &outbuffer,
  27. Null
  28. 0,
  29. 0,
  30. Boxsize,
  31. Boxsize,
  32. Null
  33. Kvimageedgeextend);
  34. if (Error) {
  35. NSLog (@ "error from convolution%ld", error);
  36. }
  37. Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb ();
  38. Cgcontextref CTX = Cgbitmapcontextcreate (
  39. Outbuffer.data,
  40. Outbuffer.width,
  41. Outbuffer.height,
  42. 8,
  43. Outbuffer.rowbytes,
  44. ColorSpace,
  45. Kcgimagealphanoneskiplast);
  46. Cgimageref imageref = Cgbitmapcontextcreateimage (CTX);
  47. UIImage *returnimage = [UIImage imagewithcgimage:imageref];
  48. Clean up
  49. Cgcontextrelease (CTX);
  50. Cgcolorspacerelease (ColorSpace);
  51. Free (pixelbuffer);
  52. Cfrelease (Inbitmapdata);
  53. Cgcolorspacerelease (ColorSpace);
  54. Cgimagerelease (IMAGEREF);
  55. return returnimage;
  56. }

iOS Gaussian blur processing

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.