IOS Add filters to pictures & use opengles dynamic rendering pictures and examples _ios

Source: Internet
Author: User

iOS add filters to pictures & use Opengles to dynamically render pictures

Add a filter to a picture in both ways: coreimage/opengles

here's how to add a filter to a picture using coreimage, mainly for the following steps:

#1. Import original pictures in ciimage format

#2. Create Cifilter Filter

#3. Render the picture in the filter with Cicontext

#4. Export a rendered picture

Reference code:

Import ciimage
  Ciimage *ciimage = [[Ciimage alloc] initwithimage:[uiimage imagenamed:@ "Hua"]];
  
  Create a filter filter
  cifilter *filter = [Cifilter filterwithname:@ "Cipixellate"];
  
  [Filter Setvalue:ciimage Forkey:kciinputimagekey];
  
  [Filter setdefaults];
  
  Ciimage *outimage = [Filter Valueforkey:kcioutputimagekey];
  
  Use Cicontext to render the picture in the filter
  cicontext *context = [Cicontext contextwithoptions:nil];
  
  Cgimageref cgimage = [Context createcgimage:outimage
                    fromrect:[outimage extent]];
  
  Export picture
  uiimage *showimage = [UIImage imagewithcgimage:cgimage];
  
  Cgimagerelease (cgimage);
  
  Uiimageview *imageview = [[Uiimageview alloc] initwithimage:showimage];
  Imageview.center    = self.view.center;
  [Self.view Addsubview:imageview];

When you want to set more than one filter, there is a new cifilter to create an additional kciinputanglekey, the code is as follows:

Import ciimage Ciimage *ciimage = [[Ciimage alloc] initwithimage:[uiimage imagenamed:@ "Hua.jpeg"]];
  
  Create a filter filter cifilter *filter = [Cifilter filterwithname:@ "Cipixellate"];
  
  [Filter Setvalue:ciimage Forkey:kciinputimagekey];
  
  [Filter SetDefaults];
  
  Ciimage *outimage = [Filter Valueforkey:kcioutputimagekey];
  
  Cifilter *filtertwo = [Cifilter filterwithname:@ "Cihueadjust"];
  
  [Filtertwo setvalue:outimage Forkey:kciinputimagekey];
  
  [Filtertwo SetDefaults]; [Filtertwo setvalue:@ (1.0f) Forkey:kciinputanglekey];
  
  If this line does not increase the new filter will not take effect ciimage *outputimage = [Filtertwo Valueforkey:kcioutputimagekey]; 
  
  Use Cicontext to render the picture in the filter cicontext *context = [Cicontext Contextwithoptions:nil];
  
  Cgimageref cgimage = [Context Createcgimage:outputimage fromrect:[outputimage extent]];
  
  Export picture UIImage *showimage = [UIImage imagewithcgimage:cgimage];
  
  Cgimagerelease (Cgimage); Uiimageview *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 to use Opengles are roughly as follows:

#1. Import the picture you want to render

#2. Get the context of opengles rendering

#3. Create a rendered Glkview buffer

#4. Create a Coreimage context

#5. Related Settings for Coreimage

#6. Start rendering and display pictures

The reference code is as follows:

Import the picture you want to render uiimage *showimage = [uiimage imagenamed:@ "Hua.jpeg"];
  
  CGRect rect = CGRectMake (0, 0, showImage.size.width, showImage.size.height);
  
  Gets the context Eaglcontext *eagcontext of the opengles rendering = [[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2];
  Creates a rendered buffer glkview *glkview = [[Glkview alloc] Initwithframe:rect Context:eagcontext];
  [Glkview binddrawable];
  
  [Self.view Addsubview:glkview]; Create a coreimage context cicontext *cicontext = [cicontext contextwitheaglcontext:eagcontext option
  
  S:@{kcicontextworkingcolorspace: [Nsnull NULL]}];
  
  Coreimage Related Settings Ciimage *ciimage = [[Ciimage alloc] initwithimage:showimage];
  
  Cifilter *filter = [Cifilter filterwithname:@ "Cisepiatone"];
  [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) fromrect:[ciimage extent]]; [Glkview display];

If you want dynamic rendering, you can dynamically adjust the Vaule value of the code through Uisilder

[Filter Setvalue:vaule Forkey:kciinputintensitykey];

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.