iOS add filters to pictures & animate images with Opengles

Source: Internet
Author: User

There are two ways to add filters to a picture: Coreimage/opengles

Here is how to use Coreimage to add a filter to a picture, mainly for the following steps:

#1. Import the original picture in ciimage format

#2. Creating a Cifilter Filter

#3. Render the image in the filter with Cicontext

#4. Exporting a rendered picture

Reference code:

1     //Import Ciimage2Ciimage *ciimage = [[Ciimage alloc] initwithimage:[uiimage imagenamed:@"Hua"]];3     4     //Create a filter filter5Cifilter *filter = [Cifilter filterwithname:@"cipixellate"];6     7 [Filter Setvalue:ciimage Forkey:kciinputimagekey];8     9 [Filter setdefaults];Ten      OneCiimage *outimage =[Filter Valueforkey:kcioutputimagekey]; A      -     //use Cicontext to render the images in the filter -Cicontext *context =[Cicontext Contextwithoptions:nil]; the      -Cgimageref Cgimage =[Context Createcgimage:outimage - fromrect:[outimage extent]]; -      +     //Export Pictures -UIImage *showimage =[UIImage imagewithcgimage:cgimage]; +      A cgimagerelease (cgimage); at      -Uiimageview *imageview =[[Uiimageview alloc] initwithimage:showimage]; -Imageview.center =Self.view.center; -[Self.view Addsubview:imageview];

What are the options for filters can be found in the following articles: HTTP://WWW.JIANSHU.COM/P/3E2CCA585CCC

When you want to set up a number of filters, out of the new to create a cifilter and additional settings Kciinputanglekey, the code is as follows:

1 //Import Ciimage2Ciimage *ciimage = [[Ciimage alloc] initwithimage:[uiimage imagenamed:@"Hua.jpeg"]];3     4     //Create a filter filter5Cifilter *filter = [Cifilter filterwithname:@"cipixellate"];6     7 [Filter Setvalue:ciimage Forkey:kciinputimagekey];8     9 [Filter setdefaults];Ten      OneCiimage *outimage =[Filter Valueforkey:kcioutputimagekey]; A      -Cifilter *filtertwo = [Cifilter filterwithname:@"Cihueadjust"]; -      the [Filtertwo setvalue:outimage Forkey:kciinputimagekey]; -      - [Filtertwo setdefaults]; -      +     [Filtertwo setvalue:@ (1.0f) forkey:kciinputanglekey];//If the new filter does not increase this line will not take effect -      +Ciimage *outputimage =[Filtertwo Valueforkey:kcioutputimagekey]; A      at     //use Cicontext to render the images in the filter -Cicontext *context =[Cicontext Contextwithoptions:nil]; -      -Cgimageref Cgimage =[Context Createcgimage:outputimage - fromrect:[outputimage extent]]; -      in     //Export Pictures -UIImage *showimage =[UIImage imagewithcgimage:cgimage]; to      + cgimagerelease (cgimage); -      theUiimageview *imageview =[[Uiimageview alloc] initwithimage:showimage]; *Imageview.center =Self.view.center; $[Self.view Addsubview:imageview];

Here's how to use Opengles to render a picture with a filter

The steps for using Opengles are as follows:

#1. Import the picture you want to render

#2. Get context for opengles rendering

#3. Creating a rendered Glkview buffer

#4. Creating a context for Coreimage

#5. Settings for Coreimage

#6. Start rendering and displaying pictures

The reference code is as follows:

1 //import a picture to render2UIImage *showimage = [UIImage imagenamed:@"Hua.jpeg"];3CGRect rect = CGRectMake (0,0, ShowImage.size.width, showImage.size.height);4     5     //Gets the context of the Opengles rendering6Eaglcontext *eagcontext =[[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2];7     8     //create a rendered buffer9Glkview *glkview =[[Glkview alloc] Initwithframe:rectTen Context:eagcontext]; One [Glkview binddrawable]; A [Self.view Addsubview:glkview]; -      -     //Create a context for the Coreimage theCicontext *cicontext =[Cicontext Contextwitheaglcontext:eagcontext -Options:@{kcicontextworkingcolorspace: [NSNullNULL]}]; -      -     //Coreimage Related Settings +Ciimage *ciimage =[[Ciimage alloc] initwithimage:showimage]; -      +Cifilter *filter = [Cifilter filterwithname:@"Cisepiatone"]; A      at [Filter Setvalue:ciimage Forkey:kciinputimagekey]; -[Filter setvalue:@ (0) Forkey:kciinputintensitykey]; -      -     //Start Rendering - [Cicontext drawimage:[filter Valueforkey:kcioutputimagekey] -Inrect:cgrectmake (0,0, Glkview.drawablewidth, Glkview.drawableheight) in fromrect:[ciimage extent]]; -      to[Glkview display];

If you want to render dynamically, you can dynamically adjust the Vaule value of the code by Uisilder

[Filter Setvalue:vaule Forkey:kciinputintensitykey];

iOS add filters to pictures & animate images with Opengles

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.