First introduce the core image, he is a very powerful image processing framework, he can allow you to easily apply a variety of filters to deal with the image, such as hue, saturation, brightness and so on ... He is a frame that uses the GPU (CPU) to process image data and video in real time. And the core image filter can be stacked together to produce multi-filter effects. Because no filters have their own unique parameters, you can modify these parameters to achieve the desired effect.
When applying the core image, pay attention to the three more general classes: Cicontext, Ciimage, Cifilter.
1.CIContext: All image processing is done in one cicontext, equivalent to the context in OpenGL;
2.CIImage: Used to save image data. At the same time, he can be constructed by pixel data;
Create a Ciimage object
UIImage *image = [UIImage imagenamed:@ "Test.png"];
NSData *data = uiimagepngrepresentation (image);
Self.ciimage = [Ciimage imagewithdata:data];
3.CIFiter: A filter class with a dictionary structure, with many filters in it. A variety of filters define their own different properties. He has a lot of kinds, can refer to http://blog.sina.com.cn/s/blog_5fb39f9101018g34.html
Print all the filter types
Nsarray *cifilter = [Cifilter filternamesincategory:kcicategorybuiltin];
NSLog (@ "Filter Name: \n%@\n Number of filters:%ld", Cifilter,cifilter.count);
Nonsense not much to say directly on the code
Create a Ciimage object
UIImage *image = [UIImage imagenamed:@ "Girl.png"];
NSData *data = uiimagepngrepresentation (image);
Self.ciimage = [Ciimage imagewithdata:data];
Create a CPU-based Cicontext object
Self.context = [Cicontext Contextwithoptions:nil];
Creating filters
Adjust hue
Self.colourfilter = [Cifilter filterwithname:@ "Cihueadjust"];
[Self.colourfilter SetDefaults];
Set the filter parameters to change the image data
When setting the parameters of the filter, you have to clarify the properties of the current filter, which can be viewed and further changed by printing the dictionary data of the corresponding filter:
NSLog (@ "cihueadjust______%@", [Self.colourfilter attributes]);
/**
Inputangle = {
Ciattributeclass = NSNumber;
Ciattributedefault = 0;
Ciattributedescription = "an angle (in radians) to use to correct the hue of an image.";
Ciattributedisplayname = Angle;
ciattributeidentity = 0;
Ciattributeslidermax = "3.141592653589793";
Ciattributeslidermin = "-3.141592653589793";
Ciattributetype = Ciattributetypeangle;
};
Inputimage = {
Ciattributeclass = Ciimage;
Ciattributedescription = "The image to use as an input image." For filters so also use a background image, this is the foreground image. ";
Ciattributedisplayname = Image;
Ciattributetype = Ciattributetypeimage;
};
*/
You can see the value range of the Inputangle input angle "-3.141592653589793-3.141592653589793"
[_colourfilter setvalue:_ciimage forkey:@ "Inputimage"];
[_colourfilter setvalue:[nsnumber Numberwithfloat:2] forkey:@ "Inputangle"];
Get the filtered picture
Ciimage *outputimage = [_colourfilter outputimage];
Convert picture
Cgimageref cgimg = [_context createcgimage:outputimage fromrect:[outputimage extent]];
UIImage *newimg = [UIImage imagewithcgimage:cgimg];
Show pictures
[_imageview setimage:newimg];
Cgimagerelease (CGIMG);
specific demo link https://github.com/White007/CIFilterDemo.git
Powerful core image frame with various filters for image processing