One of the IOS Core image

Source: Internet
Author: User

In the project to achieve the effect of Gaussian blur, today saw the core image of this piece of content, mainly including Ciimage, Cifilter, Cicontext, Cidetector (detection), Cifeature (features) and other classes.

Today, we record the use of three classes of Ciimage, Cifilter and Cicontext.

First, the basic filter effect requires the following steps

1. Create a Ciimage object

2. Create a Cicontext object to use as a canvas

3. Create a Cifilter object

4. Output filter

Ii. creating an API for the above three objects

1. Create Ciimage objects mainly by the following methods (methods have a lot of specific view class Ciimage)

+ (Ciimage *) Imagewithcgimage: (cgimageref) image; + (Ciimage *) Imagewithcglayer: (cglayerref) layer; + (Nullable Ciimage *) Imagewithcontentsofurl: (Nsurl *) URL; + (Nullable Ciimage *) Imagewithdata: (NSData *) data; - (Instancetype) Initwithcgimage: (cgimageref) image; -(Nullable Instancetype) Initwithdata: (NSData *) data; -(Nullable Instancetype) Initwithcontentsofurl: (Nsurl *) URL; -(Instancetype) Initwithcolor: (Cicolor *) color;

2. Create a Cicontext object

Cicontext Constructor Contextwithoptions: The input is a nsdictionary. It specifies options, including the color format and whether the content should run on the CPU or GPU.

Cicontext *context=[cicontext Contextwithoptions:nil];

There are some other methods in this class

- (void) DrawImage: (Ciimage *image atpoint: (cgpoint) atpoint fromrect: (cgrect) Fromrect ns_deprecated (10_4,10_8, 5_0,6_0);/*Render the rectangle ' fromrect ' of ' image ' to the rectangle ' inrect ' in the * context ' s destination.*/- (void) DrawImage: (Ciimage *) Image inrect: (cgrect) inrect fromrect: (cgrect) fromrect;/*Render the region ' fromrect ' of image ' image ' to a temporary buffer using * The context, then create and return a n EW coregraphics image with * the results. The caller is responsible for releasing the returned * image. */-(Cgimageref) Createcgimage: (Ciimage *) Image fromrect: (cgrect) fromrectcf_returns_retained;/*Create A new cgimage from the specified subrect of the image. If * Non-nil The new image is created in the specified format and * colorspace. */-(Cgimageref) Createcgimage: (Ciimage *) Image fromrect: (cgrect) fromrect format: (ciformat) format COLORSP Ace: (Nullable cgcolorspaceref) ColorSpace

3. Create a Cifilter object

1. Creating Filter objects

+ (Nullable Cifilter *) Filterwithname: (NSString *) name;/** Creates a new filter of type ' name '. The filter ' s input parameters is set from the list of key-value pairs which must is nil-terminated. On OS X, any of the filter input parameters not specified in the list would be undefined. On IOS, any of the filter input parameters not specified in the list is set to default values. */+ (Nullable Cifilter *) Filterwithname: (NSString *) name Keysandvalues:key0, ... Ns_requires_nil_termination ns_swift_unavailable ("");/** Creates a new filter of type ' name '. The filter ' s input parameters is set from the dictionary of Key-value pairs. On OS X, any of the filter input parameters not specified in the dictionary would be undefined. On IOS, any of the filter input parameters not specified in the dictionary is set to default values. */+ (Nullable Cifilter *) Filterwithname: (NSString *) name Withinputparameters: (Nullable ci_dictionary (NSString*,ID) *)paramsNs_available (10_10, 8_0);

2, the above to create filter objects need filtername, how to see the name and the properties of each Cifilter object?

Cifilter *filter = [Cifilter filterwithname:@ "cigaussianblur"];    NSLog (@ "%@", filter.attributes);

Output the following code in the above code

 -- One-Ten  -: the:46.697coreimage[22672:246714] {    "Ciattributefilteravailable_mac"="10.4"; "Ciattributefilteravailable_ios"=6; Ciattributefiltercategories=(Cicategoryblur, Cicategorystillimage, Cicategoryvideo, Cicategorybuiltin); Ciattributefilterdisplayname="Gaussian Blur"; Ciattributefiltername=Cigaussianblur; Ciattributereferencedocumentation="Http://developer.apple.com/cgi-bin/apple_ref.cgi?apple_ref=//apple_ref/doc/filter/ci/CIGaussianBlur"; Inputimage={Ciattributeclass=Ciimage; Ciattributedescription="The image to use as a input image. For filters so also use a background image, this is the foreground image."; Ciattributedisplayname=Image; Ciattributetype=Ciattributetypeimage;    }; Inputradius={Ciattributeclass=NSNumber; Ciattributedefault=Ten; Ciattributedescription="The radius determines how many pixels is used to create the blur. The larger the radius, the blurrier the result."; Ciattributedisplayname=Radius; Ciattributeidentity=0; Ciattributemin=0; Ciattributeslidermax= -; Ciattributeslidermin=0; Ciattributetype=Ciattributetypescalar; };}

From the above output we can see that the filter has ciattributefiltercategories, Ciattributefilterdisplayname, Ciattributefiltername, InputImage , Inputradius, and other properties.

In the ciattributefiltercategories, you can see the filter has Cicategoryblur, cicategorystillimage,cicategoryvideo, Cicategorybuiltin species, In the Cifilter class, there are two ways to iterate through all the filter names

/* */+ (Ci_array (nsstring*) *) Filternamesincategory: (Nullable NSString *) category;  /*  */+ (Ci_array (nsstring*) *) Filternamesincategories: (Nullable Ci_array (nsstring*) *) categories;

After obtaining the filter name, you can view the properties of the filter object through the Attributes property, and set the property value by KVC

 //similar to UIButton by simple parameters to determine the Cifilter sub-class simple factoryCifilter *filter = [Cifilter filterwithname:@"Ciaffinetransform"]; //NSLog (@ "%@", filter.attributes); //Enter the image into the filter, for example: The filter is a container with a function, anything put in, take out the time will be attached to the effect. KVC[Filter Setvalue:ciimage Forkey:@"Inputimage"]; [Filter SetValue: [Nsvalue valuewithcgaffinetransform:cgaffinetransformmakerotation ( -)] Forkey:@"Inputtransform"];

4. Output filter

1, through the Imagewithciimage method of UIImage

// To remove a picture from a filter container there is  also a way to output: Use Cicontext  do not know the pros and cons of    these two, welcome message //ciimage *new = [Filter Valueforkey:kcioutputimagekey];    Ciimage *outputimage = filter.outputimage;         *img = [[Uiimageview alloc] initWithFrame:self.view.frame];     = [UIImage imagewithciimage:outputimage];     // img.image=[uiimage imagewithciimage:outputimage scale:30 Orientation:uiimageorientationdown];    [Self.view addsubview:img];

2, through the UIImage Imagewithcgimage method to use this method Cicontext

// ciimage *new = [Filter Valueforkey:kcioutputimagekey];    Ciimage *outputimage = filter.outputimage;         *context=[Cicontext Contextwithoptions:nil];     =[Context createcgimage:outputimage fromrect:[outputimage extent];     *img = [[Uiimageview alloc] initWithFrame:self.view.frame];    Img.image=[UIImage imagewithcgimage:cgimg];    [Self.view addsubview:img];    Cgimagerelease (cgimg);

In both of these methods, a cicontext is generated for the first call each time. Cicontext could have been reused to improve performance and efficiency.

5. Complete code

////VIEWCONTROLLER.M//Coreimage////Created by City--online on 15/11/10.//copyright©2015 year City--online. All rights reserved.//#import "ViewController.h"#import<CoreImage/CoreImage.h>@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Ciimage*inputimg = [[Ciimage alloc]initwithcgimage:[uiimage imagenamed:@"1.jpg"].        Cgimage]; //similar to UIButton by simple parameters to determine the Cifilter sub-class simple factoryCifilter *filter = [Cifilter filterwithname:@"Ciaffinetransform"]; //Output PropertiesNSLog (@"%@", filter.attributes); //Enter the image into the filter, for example: The filter is a container with a function, anything put in, take out the time will be attached to the effect. KVC[Filter setvalue:inputimg Forkey:@"Inputimage"]; [Filter SetValue: [Nsvalue valuewithcgaffinetransform:cgaffinetransformmakerotation ( -)] Forkey:@"Inputtransform"]; //To remove a picture from a filter container there is also an output mode: Use Cicontext do not know what the pros and cons of these two, welcome message//ciimage *new = [Filter Valueforkey:kcioutputimagekey];Ciimage *outputimage =Filter.outputimage; //by Cicontext context, Imagewithcgimage output filterCicontext *context=[Cicontext Contextwithoptions:nil]; Cgimageref cgimg=[Context Createcgimage:outputimage fromrect:[outputimage extent]; Uiimageview*img =[[Uiimageview alloc] initWithFrame:self.view.frame]; Img.image=[UIImage imagewithcgimage:cgimg];    [Self.view addsubview:img];    Cgimagerelease (CGIMG); //traverse the filter name under each filterNSLog (@"%@", [Cifilter filternamesincategories:@[@"Cicategoryblur"]]); NSLog (@"%@", [Cifilter filternamesincategories:@[@"Cicategoryvideo"]]); NSLog (@"%@", [Cifilter filternamesincategories:@[@"Cicategorystillimage"]]); NSLog (@"%@", [Cifilter filternamesincategories:@[@"Cicategorybuiltin"]]); }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

6. Effects

One of the IOS Core image

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.