iOS image processing (9) using the Coreimage filter

Source: Internet
Author: User

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

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.