Objective
Girls like to use the Beauty Map tool for the beauty of the picture, when nothing recently, specifically for someone to write a custom image filter generator, installed on the phone to complete the custom filter rendering photos. The app is unique, though simple and complex.
JH Law:
Magic Mirror: The most beautiful woman is your wife
Magic Mirror: Programmers are not wood people
Core technology
The basic idea of image filter core technology is as follows:
Core technology processes
Process 1, create an image processing tool class
Note: This class instance includes an image processing method that is used to generate a well-processed image in the incoming original image and a color matrix.
@interface JHFeilterManager : NSObject@property (nonatomic,copy)imageBlock imageBLOCK;- (UIImage *)createImageWithImage:(UIImage *)inImage colorMatrix:(const float *)f;
2. Gets an array of RGBA values for each pixel point of the image
Note: The C method returns a pointer to an array in which each of the four elements is an RGBA numeric value (0-255) of a pixel on the image, with an unsigned char because its exact value range is 0-255
static unsigned char *RequestImagePixelData(UIImage * inImage){ CGImageRef img = [inImage CGImage]; CGSize size = [inImage size]; //使用上面的函数创建上下文 CGContextRef cgctx = CreateRGBABitmapContex(img); CGRect rect = {{0,0},{size.width,size.height}}; //将目标图像绘制到指定的上下文,实际为上下文内的bitmapData。 CGContextDrawImage(cgctx, rect, img); unsigned char *data = CGBitmapContextGetData(cgctx); //释放上面的函数创建的上下文 CGContextRelease(cgctx); cgctx = NULL; return data;}
3, adjust the RGBA value of a pixel point
Note: The following method passes the parameter as a numeric pointer and a color matrix, which adjusts the value of the pointer to the address store by the color matrix
static void changeRGB(int *red,int* green,int*blue,int*alpha ,const float *f){ int redV = *red; int greenV = *green; int blueV = *blue; int alphaV = *alpha; //色值重新计算 计算规则如下 *red = f[0] * redV + f[1]*greenV + f[2]*blueV + f[3] * alphaV + f[4]; *green = f[5] * redV + f[6]*greenV + f[7]*blueV + f[8] * alphaV+ f[9]; *blue = f[10] * redV + f[11]*greenV + f[12]*blueV + f[11] * alphaV+ f[14]; *alpha = f[15] * redV + f[16]*greenV + f[17]*blueV + f[18] * alphaV+ f[19]; *red < 0 ? (*red = 0):(0); *red > 255 ? (*red = 255):(0); *green < 0 ? (*green = 0):(0); *green > 255 ? (*green = 255):(0); *blue < 0 ? (*blue = 0):(0); *blue > 255 ? (*blue = 255):(0); *alpha < 0 ? (*alpha = 0):(0); *alpha > 255 ? (*alpha = 255):(0);}
4, traverse each pixel, adjust the color value Note!!!
In the following method, do not immediately release the BitmapData memory space pointer generated by the malloc method, (some friends may think that the memory space address has been given to the bitmap context can be released immediately, but in fact, because the bitmap context in the subsequent image rendering, still need this piece of memory, Therefore can not immediately release memory here, the previous reading of several blogs simply do not release memory, it will lead to memory leaks, processing some high-definition images, cell phone memory will easily soar to more than 1G, and cause the program to hang up, otherwise it will cause the contents of the bitmap context data can not be normal, resulting in a picture generation failure, Here a global memory pointer is needed to point to it, and the memory is freed when appropriate.
This method needs to pass in a raw picture information and a color matrix, the color matrix determines the rendering effect of the image, so different filter effects can be set by different color matrix conversion, if you do not understand the color matrix, click here to understand
-(UIImage *) Createimagewithimage: (UIImage *) inimage ColorMatrix: (const float *) f{/* Picture bit image Value Array */unsigned char *IMGP Ixel = Requestimagepixeldata (inimage); Cgimageref inimageref = [Inimage cgimage]; /* Gets the number of horizontal and vertical pixels */Long W = cgimagegetwidth (inimageref); Long h = cgimagegetheight (inimageref); int wOff = 0; int pixoff = 0; /* Iterate through the modifier bit image value */for (long y = 0; y
5. UI Setup
UI interface is very simple, is a large picture, 20 color matrix information Entry box and two buttons, the code is not too much to repeat, there is a need to download the demo here, like the words do not give a star oh.
Results show
The molded demo is like this!
Gabriella, reprint please from SUN's BLOG-focus on Internet knowledge, share the spirit of the Internet!.
The correct position of the programmer to please his girlfriend---Tips (iOS beauty article)
The right posture for programmers to please their girlfriends---Tips (iOS beauty article)