The realization method of IOS image Blur effect _ios

Source: Internet
Author: User

This article for everyone to share the iOS image blur effect of three ways to achieve, for your reference, the specific content as follows

1. Realize the effect in order as follows: Original picture, iOS8 effect, Core image effect, vimage effect

-

2. Code

#import "ViewController.h" #import <Accelerate/Accelerate.h> @interface Viewcontroller () @end @implement 
 ation Viewcontroller-(void) viewdidload {[Super viewdidload]; 
  
Self.view.backgroundColor = [Uicolor colorwithpatternimage:[uiimage imagenamed:@ "background"]]; 
[Self ios8blurimageimplement]; 
 [Self coreimageimplement]; 
[Self vimageimplement]; }//IOS8 using the system's own processing Method-(void) ios8blurimageimplement {uiblureffect *beffect = [Uiblureffect effectwithstyle:uiblure 
 Ffectstylelight]; 
 Uivisualeffectview *view = [[Uivisualeffectview alloc] initwitheffect:beffect]; 
 View.frame = Self.view.bounds; 
[Self.view Addsubview:view]; 
  
 ///Use Coreimage to implement picture Blur-(void) coreimageimplement{cicontext *context = [Cicontext Contextwithoptions:nil]; 
 Nserror *error = nil; 
 NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "background" oftype:@ "PNG"]; NSData *imagedata = [NSData datawithcontentsoffile:filepath options:nsdatareadinguncached Error:&error]; 
 NSData *imagedata = [NSData datawithcontentsoffile:@ "Background.png"]; 
 Ciimage *image = [Ciimage imagewithdata:imagedata]; 
 Cifilter *filter = [Cifilter filterwithname:@ "Cigaussianblur"]; 
 [Filter Setvalue:image Forkey:kciinputimagekey]; 
 [Filter setvalue:@2.0f forkey:@ "Inputradius"]; 
 Ciimage *result = [Filter Valueforkey:kcioutputimagekey]; 
 Cgimageref outimage = [Context Createcgimage:result fromrect:[result extent]]; 
  
 UIImage *bluerimage = [UIImage imagewithcgimage:outimage]; 
 Uiimageview *imageview = [[Uiimageview alloc] initwithimage:bluerimage]; 
 Imageview.frame = Self.view.bounds; 
[Self.view Addsubview:imageview]; ///using Vimage API to implement picture blur//iOS5.0 added Vimage API can be used, it belongs to Accelerate.framework, so if you want to use it to incorporate this Framework into the project. 
The fuzzy algorithm uses the vimageboxconvolve_argb8888 function. 
 -(void) vimageimplement {uiimage *image = [uiimage imagenamed:@ "background"]; 
 UIImage *blurimage = [self blurryimage:image withblurlevel:0.5]; Self.view.backgroundColor =[Uicolor Colorwithpatternimage:blurimage]; }-(UIImage *) Blurryimage: (UIImage *) image withblurlevel: (CGFloat) Blur {if (Blur < 0.F | | Blur > 1.F) {bl 
 ur = 0.5f; 
 int boxsize = (int) (blur * 100); 
  
 Boxsize = boxsize-(boxsize% 2) + 1; Cgimageref img = image. 
  
 Cgimage; 
 Vimage_buffer Inbuffer, Outbuffer; 
  
 Vimage_error Error; 
  
 Voidvoid *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, Outbuffe R.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;  } @end

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.