iOS picture Blur effect

Source: Internet
Author: User


Join Coreimage.framework Coregraphic.framework and other libraries


when used, introduce: #import<Accelerate/Accelerate.h> support for iOS 5.0 and above.



-(void) show

{

UIImage* img = [selfgetblurimage: [UIImageimagenamed:@ " Default-568h.png "];

[_bgimageview setimage: img];

}


-(UIImage*) Getblurimage: (UIImage*) image

{

return [selfgaussblur:0.2andimage: image];

}


-(UIImage*) Gaussblur: (cgfloat) blurlevel andimage: (UIImage*) originimage

{

Blurlevel = MIN(1.0,MAX(0.0, blurlevel));


//int boxsize = (int) (Blurlevel * 0.1 * MIN (Self.size.width, self.size.height));

int boxsize = ; // The degree of ambiguity.

Boxsize = boxsize-(boxsize% 2) + 1;


nsdata *imagedata = uiimagejpegrepresentation(originimage, 1);

UIImage *tmpimage = [UIImageimagewithdata: ImageData];


cgimageref img = tmpimage. Cgimage;

vimage_buffer inbuffer, Outbuffer;

vimage_error Error;

void *pixelbuffer;


//create vimage_buffer with data from Cgimageref

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);


//create vimage_buffer for output

Pixelbuffer = malloc(cgimagegetbytesperrow(IMG) *cgimagegetheight(img));


Outbuffer. data = Pixelbuffer;

Outbuffer. width = cgimagegetwidth(IMG);

Outbuffer. height = cgimagegetheight(IMG);

Outbuffer. rowbytes = cgimagegetbytesperrow(IMG);


nsinteger windowr = boxsize/2;

cgfloat sig2 = WINDOWR/ 3.0;

if (windowr>0) {sig2 =-1/(2*sig2*sig2);}


int16_t *kernel = (int16_t*)malloc(boxsize*sizeof(int16_t));

int32_t sum = 0;

for (nsinteger i=0; i<boxsize; ++i) {

Kernel[i] = 255*exp(sig2* (I-WINDOWR) * (I-WINDOWR));

Sum + = Kernel[i];

}

Free (kernel);

//convolution

Error = vimageconvolve_argb8888(&inbuffer, &outbuffer,NULL, 0, 0, kernel , Boxsize, 1, sum, NULL, kvimageedgeextend);

Error = vimageconvolve_argb8888(&outbuffer, &inbuffer,NULL, 0, 0, Kernel, 1, boxsize, Sum, NULL, kvimageedgeextend);

Outbuffer = Inbuffer;


if (Error) {

//nslog (@ "error from convolution%ld", error);

}


cgcolorspaceref colorspace =Cgcolorspacecreatedevicergb();

cgcontextref ctx = cgbitmapcontextcreate(outbuffer. Data,

Outbuffer. width,

Outbuffer. height,

8,

Outbuffer. Rowbytes,

ColorSpace,

kcgbitmapalphainfomask &kcgimagealphanoneskiplast);

cgimageref imageref =cgbitmapcontextcreateimage(CTX);

UIImage *returnimage = [UIImageimagewithcgimage: Imageref];


//clean up

cgcontextrelease(CTX);

cgcolorspacerelease(colorspace);

Free (Pixelbuffer);

Cfrelease (Inbitmapdata);

Cgimagerelease (IMAGEREF);


return returnimage;

}



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.