Sometimes it is difficult or impossible to work on an image alone to get the effect we want. The birth of a good filter effect, often through a lot of complex steps, fine-tuning, picture application effect observation and a lot of layer overlay.
I found some common blending algorithms on jswidget, which correspond to some common blending modes, and through these blend modes, we can specify how the two images are blended.
But before we do, we need a plain color image and a gradient image to help:
[CPP]View Plaincopy
- + (UIImage *) Imagewithcolor: (Uicolor *) color size: (cgsize) size
- {
- //HTTP://STACKOVERFLOW.COM/QUESTIONS/1213790/HOW-TO-GET-A-COLOR-IMAGE-IN-IPHONE-SDK
- //create A context of the appropriate size
- Uigraphicsbeginimagecontext (size);
- Cgcontextref CurrentContext = Uigraphicsgetcurrentcontext ();
- //build A rect of appropriate size at Origin 0,0
- CGRect fillRect = CGRectMake (0, 0, size.width, size.height);
- //set the Fill color
- Cgcontextsetfillcolorwithcolor (CurrentContext, color. Cgcolor);
- //fill The color
- Cgcontextfillrect (CurrentContext, fillRect);
- //snap The picture and close the context
- UIImage *colorimage = Uigraphicsgetimagefromcurrentimagecontext ();
- Uigraphicsendimagecontext ();
- return colorimage;
- }
[CPP]View Plaincopy
- + (UIImage *) Imagewithgradient: (UIImage *) image startcolor: (Uicolor *) StartColor EndColor: (Uicolor *) EndColor
- {
- Uigraphicsbeginimagecontextwithoptions (Image.size, NO, Image.scale);
- Cgcontextref context = Uigraphicsgetcurrentcontext ();
- CGCONTEXTTRANSLATECTM (context, 0, image.size.height);
- CGCONTEXTSCALECTM (Context, 1.0,-1.0);
- Cgcontextsetblendmode (context, kcgblendmodenormal);
- CGRect rect = CGRectMake (0, 0, image.size.width, image.size.height);
- Cgcontextdrawimage (context, rect, image. Cgimage);
- //Create gradient
- Nsarray *colors = [Nsarray arraywithobjects: (ID) endcolor.cgcolor, (ID) startcolor.cgcolor, nil];
- Cgcolorspaceref space = Cgcolorspacecreatedevicergb ();
- Cggradientref gradient = cggradientcreatewithcolors (space, (CFARRAYREF) colors, NULL);
- //Apply gradient
- Cgcontextcliptomask (context, rect, image. Cgimage);
- Cgcontextdrawlineargradient (context, gradient, Cgpointmake (0,0), Cgpointmake (0, Image.size.height), 0);
- UIImage *gradientimage = Uigraphicsgetimagefromcurrentimagecontext ();
- Uigraphicsendimagecontext ();
- Cggradientrelease (gradient);
- Cgcolorspacerelease (space);
- return gradientimage;
- }
And the transparency filter mentioned in the first article (on the alpha value of the scope pixel) is ineffective and can be achieved by quartz 2D:
[CPP]View Plaincopy
- -(UIImage *) Setalpha: (cgfloat) Alpha
- {
- //Http://stackoverflow.com/questions/5084845/how-to-set-the-opacity-alpha-of-a-uiimage
- Uigraphicsbeginimagecontextwithoptions (Self.size, NO, 0.0f);
- Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
- CGRect area = CGRectMake (0, 0, self.size.width, self.size.height);
- CGCONTEXTSCALECTM (CTX, 1,-1);
- CGCONTEXTTRANSLATECTM (CTX, 0,-area.size.height);
- Cgcontextsetblendmode (CTX, kcgblendmodemultiply);
- Cgcontextsetalpha (CTX, Alpha);
- Cgcontextdrawimage (CTX, area, self.) Cgimage);
- UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
- Uigraphicsendimagecontext ();
- return newimage;
- }
On this basis, through the following four lines of code, you can get four different effects:
[CPP]View Plaincopy
- return [[UIImage Imagewithcolor:[uicolor Purplecolor] size:originImage.size] changeopacitybyfactor:0.5];;
[CPP]View Plaincopy
- return [UIImage imagewithgradient:originimage startcolor:[uicolor Whitecolor] Endcolor:[uicolor YellowColor]];
[CPP]View Plaincopy
- return [[Originimage Tintwithmaxrgba: (RGBA) {Minrgba, overlaywithimage:[[uiimage): (RGBA) {[] Imagewithcolor:[uicolor Purplecolor] size:originImage.size] changeopacitybyfactor:0.3];
[CPP]View Plaincopy
- return [Originimage softlightwithimage:[[uiimage imagewithcolor:[uicolor Yellowcolor] size:originImage.size] changeopacitybyfactor:0.8]];
Among them, the overlay algorithm is as follows:
[CPP]View Plaincopy
- Double Calcoverlay (float B, float t)
- {
- return (b > 128.0f) 255.0f-2.0f * (255.0f-t) * (255.0f-b)/255.0f: (b * t * 2.0f)/255.0f;
- }
- void Filteroverlay (UInt8 *pixelbuf, UInt8 *pixedblendbuf, UInt32 offset, void *context)
- {
- int r = offset;
- int g = offset+1;
- int b = offset+2;
- int red = Pixelbuf[r];
- int green = Pixelbuf[g];
- int blue = pixelbuf[b];
- int blendred = Pixedblendbuf[r];
- int blendgreen = pixedblendbuf[g];
- int blendblue = pixedblendbuf[b];
- Pixelbuf[r] = Safecolor (Calcoverlay (red, blendred));
- PIXELBUF[G] = Safecolor (Calcoverlay (green, blendgreen));
- PIXELBUF[B] = Safecolor (Calcoverlay (blue, blendblue));
- }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Image processing in iOS (iii)--mixed operation