Coreimage is a new framework in IOS5 that allows us to easily manipulate images with a variety of special effects.
The main process for using Coreimage is as follows (you need to add the Coreimage framework to your project):
1 Creating a Cicontext,ios system provides 3 ways to create a cicontext
A. CPU-based
Cicontext *context = [Cicontext contextwithoption:@{kcicontextusesoftwarerenderer: @YES}];
B. GPU-based
Cicontext *context = [Cicontext Contextwithoption:nil];
The most common is also a good way of performance, the disadvantage is that the GPU-based cicontext can not be accessed across the application, such as open Uiimagepickercontroller to select a picture, if directly in the delegate method using Cicontext, will be automatically reduced to CPU-based, So you can save the picture in the delegate method and call Cicontext in the main class to handle it.
C. Based on OpenGL optimization, real-time performance is achieved
Eaglcontext *eaglcontext = [[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2];
Cicontext *context = [Cicontext contextwitheaglcontext:eaglcontext];
2 Creating a Cifilter filter
3 Create a Ciimage picture as a source picture, you can create it using Nsurl,uiimage and NSData
4 set Ciimage to Cifilter inputimage and set different parameters according to different filters
5 through the Cifilter to get the filter after the conversion of the picture, the picture format is Ciimage, through Cicontext can get cgimage
The following example shows a pixel-drawn filter
- (void) viewdidload{[Super Viewdidload]; //all filters and supported properties can be printed outNsarray *filters =[Cifilter Filternamesincategory:kcicategorybuiltin]; for(NSString *filternameinchfilters) {Cifilter*filter =[Cifilter Filterwithname:filtername]; NSLog (@"%@,%@", filtername,[filter attributes]); } //Create a GPU-based CicontextCicontext *context =[Cicontext Contextwithoptions:nil]; //Creating FiltersCifilter *filter = [Cifilter filterwithname:@"cipixellate"]; //Create CiimageCiimage *sourceimage = [ciimage imagewithcontentsofurl:[nsurl fileurlwithpath:[[nsbundle MainBundle] PathForResource:@"Girl"OfType:@"jpg"]]]; //set Ciimage as the source picture[Filter Setvalue:sourceimage Forkey:@"Inputimage"]; //Set Filter parameters (pixel size)[Filter setvalue:@ theForkey:@"Inputscale"]; //Get output PictureCiimage *outputimage =[Filter outputimage]; Cgimageref Cgimage=[Context Createcgimage:outputimage fromrect:[uiscreen mainscreen].bounds]; UIImage*image =[UIImage Imagewithcgimage:cgimage]; //called create created, need releasecgimagerelease (cgimage); Uiimageview*imageview =[[Uiimageview alloc] initwithimage:image]; [Self.view Addsubview:imageview]; }
iOS image processing (9) using the Coreimage filter