Transferred from: http://prolove10.blog.163.com/blog/static/138411843201391401054305/
Original:
?:
? Implementation:
First you need to importAccelerate.framework.
then add two files to your project.
Uiimage+imageeffects.h
#import @interfaceUIImage (imageeffects)-(uiimage*) applylighteffect;-(uiimage*) applyextralighteffect;-(uiimage*) applydarkeffect;-(uiimage*) Applytinteffectwithcolor: (uicolor*) tintcolor;-(uiimage*) ApplyBlurWithRadius: ( CGFloat) Blurradius Tintcolor: (uicolor*) Tintcolor saturationdeltafactor: (cgfloat) saturationdeltafactor maskImage: ( uiimage*) Maskimage; @end
Uiimage+imageeffects.m
#import "Uiimage+imageeffects.h" #import #import @implementationUIImage (imageeffects)-(uiimage*) applylighteffect{ Uicolor*tintcolor =[uicolor colorwithwhite:1.0 alpha:0.3];return[self applyblurwithradius:30 tintColor:tintColor saturationdeltafactor:1.8 Maskimage:nil];} -(uiimage*) Applyextralighteffect{uicolor*tintcolor =[uicolor colorwithwhite:0.97 alpha:0.82];return[self Applyblurwithradius:20 tintcolor:tintcolor saturationdeltafactor:1.8 Maskimage:nil];} -(uiimage*) Applydarkeffect{uicolor*tintcolor =[uicolor colorwithwhite:0.11 alpha:0.73];return[self Applyblurwithradius:20 tintcolor:tintcolor saturationdeltafactor:1.8 Maskimage:nil];} -(uiimage*) Applytinteffectwithcolor: (uicolor*) tintcolor{constcgfloateffectcoloralpha=0.6; Uicolor*effectcolor = Tintcolor;int componentcount =cgcolorgetnumberofcomponents (TintColor.CGColor); Componentcount ==2) {cgfloat b;if ([Tintcolor getwhite:&b alpha:null]) {effectcolor =[uicolor colorWithWhite:b Alpha : Effectcoloralpha];}} Else{cgfloat R, G, b;if ([tintColor getred:&r green:&g blue:&b alpha:null]) {effectcolor =[uicolor colorwithred:r green:g blue:b Alpha: Effectcoloralpha];}} Return[self applyblurwithradius:10 tintcolor:effectcolor saturationdeltafactor:-1.0 maskImage:nil];} -(uiimage*) Applyblurwithradius: (cgfloat) Blurradius Tintcolor: (uicolor*) Tintcolor saturationdeltafactor: (CGFloat) Saturationdeltafactor maskimage: (uiimage*) maskimage{//Check pre-conditions.if (self.size.width <1| | Self.size.height <1) {NSLOG @ * * * * Error:invalid Size: (%.2f x%.2f). Both dimensions must be >= 1:%@ ", self.size.width,self.size.height,self); returnnil;} if (!self. Cgimage) {NSLog (@ "* * * * Error:image must is backed by a cgimage:%@", self); returnnil;} if (Maskimage &&!maskimage.cgimage) {NSLog (@ "* * * error:maskimage must be backed by a cgimage:%@", maskimage); Retu Rnnil;} CGRect Imagerect ={cgpointzero,self.size}; Uiimage*effectimage =self; BOOL Hasblur = blurradius > __flt_epsilon__; BOOL Hassaturationchange = fabs (saturationdeltafactor-1.) > __flt_epsilon__;if (Hasblur | | hassaturationchange) {uigraphicsbeginimagecontextwithoptions (self.size, NO,[[ UIScreen Mainscreen]); Cgcontextref Effectincontext =uigraphicsgetcurrentcontext (); CGCONTEXTSCALECTM (effectincontext,1.0,-1.0); CGCONTEXTTRANSLATECTM (Effectincontext,0,-self.size.height); Cgcontextdrawimage (Effectincontext, imagerect,self. cgimage); Vimage_buffer effectinbuffer;effectinbuffer.data =cgbitmapcontextgetdata (effectInContext); Effectinbuffer.width =cgbitmapcontextgetwidth (effectincontext); Effectinbuffer.height =CGBitmapContextGetHeight ( Effectincontext); effectinbuffer.rowbytes =cgbitmapcontextgetbytesperrow (Effectincontext); Uigraphicsbeginimagecontextwithoptions (Self.size, No,[[uiscreen Mainscreen] scale]); Cgcontextref Effectoutcontext =uigraphicsgetcurrentcontext (); Vimage_buffer effectoutbuffer;effectoutbuffer.data = Cgbitmapcontextgetdata (Effectoutcontext); Effectoutbuffer.width =cgbitmapcontextgetwidth (EffectOutContext); Effectoutbuffer.height =CGBITMAPCONTEXTGEtheight (Effectoutcontext); effectoutbuffer.rowbytes =cgbitmapcontextgetbytesperrow (EffectOutContext); if (HasBlur ) {//A description The Compute the box kernel width from the gaussian//radius (aka Standard deviation) appears in T He SVG spec://http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement////For larger values of ' s ' (S->= 2.0), an Approximation can be used:three//successive box-blurs build a piece-wise quadratic convolution kernel, which//Approxim Ates the Gaussian kernel to within roughly 3%.////let D = floor (S * 3*SQRT (2*PI)/4 + 0.5)///... if d is odd, use three Box-blurs of size ' d ', centered on the output pixel.//cgfloat Inputradius = Blurradius *[[uiscreen mainscreen] scale]; Nsuinteger radius = floor (Inputradius *3.* sqrt (M_PI)/4+0.5), if (radius%2!=1) {radius +=1;//force radius to is odd so t Hat the three Box-blur methodology works.} vimageboxconvolve_argb8888 (&effectinbuffer,&effectoutbuffer, null,0,0, radius, radius,0, kvImageEdgeExtend ); vimageboxconvolve_argb8888 (&effectoutbuffer,&effectinbuffer, null,0,0, radius, radius,0, kvImageEdgeExtend) ; vimageboxconvolve_argb8888 (&effectinbuffer,&effectoutbuffer, null,0,0, radius, radius,0, Kvimageedgeextend);} BOOL effectimagebuffersareswapped = no;if (hassaturationchange) {cgfloat s = saturationdeltafactor; CGFloat floatingpointsaturationmatrix[]={0.0722+0.9278* s,0.0722-0.0722* s,0.0722-0.0722* s,0,0.7152-0.7152* S, 0.7152+0.2848* s,0.7152-0.7152* s,0,0.2126-0.2126* s,0.2126-0.2126* s,0.2126+0.7873* s,0,0,0,0,1,};constint32_t divisor = 256; Nsuinteger matrixsize =sizeof (Floatingpointsaturationmatrix)/sizeof (floatingpointsaturationmatrix[0]); int16_t Saturationmatrix[matrixsize];for (Nsuinteger i =0; i < matrixsize;++i) {saturationmatrix[i]= (int16_t) roundf ( floatingpointsaturationmatrix[i]* divisor);} if (Hasblur) {vimagematrixmultiply_argb8888 (&effectoutbuffer,&effectinbuffer, SaturationMatrix, divisor, NULL, NULL, kvimagenoflags); effectimagebuffersareswapped = YES;} else{vimagematrixmultiply_argb8888 (&effectinbuffer,&effectoutbuffer, Saturationmatrix, divisor, NULL, NULL , kvimagenoflags);}} if (!effectimagebuffersareswapped) effectimage =uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); if (effectimagebuffersareswapped) Effectimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();} Set up output context. Uigraphicsbeginimagecontextwithoptions (Self.size, No,[[uiscreen Mainscreen] scale]); Cgcontextref Outputcontext =uigraphicsgetcurrentcontext (); CGCONTEXTSCALECTM (outputcontext,1.0,-1.0); CGCONTEXTTRANSLATECTM (outputcontext,0,-self.size.height);//Draw base image. Cgcontextdrawimage (Outputcontext, imagerect,self. Cgimage),//Draw effect Image.if (Hasblur) {cgcontextsavegstate (Outputcontext), if (maskimage) {Cgcontextcliptomask ( Outputcontext, Imagerect, maskimage.cgimage);} Cgcontextdrawimage (Outputcontext, Imagerect, effectimage.cgimage); Cgcontextrestoregstate (Outputcontext);} Add in color Tint.if (tintcolOR) {cgcontextsavegstate (outputcontext); Cgcontextsetfillcolorwithcolor (Outputcontext, Tintcolor.cgcolor); Cgcontextfillrect (Outputcontext, imagerect); Cgcontextrestoregstate (Outputcontext);} Output image is ready. Uiimage*outputimage =uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return outputimage;} @end
Call:
Uiimageview*me =[[uiimageview alloc] Initwithframe:cgrectmake (10,480,614,381)]; [Me setimage:[[uiimage imagenamed:@ "me.png"] applyblurwithradius:5 tintcolor:[uicolor colorWithWhite:1 alpha:0.2] saturationdeltafactor:1.8 Maskimage:nil]]; [Self.view Addsubview:me];
Ok! So easy!
IOS7 Wool Glass effect