Ios-coreimage Simple to use

Source: Internet
Author: User

Coreimage is an image frame that is created based on the OpenGL top layer and the shader is used to process the image at the bottom, which means that it leverages the GPU to process the image based on hardware acceleration. There are many filters in the coreimage that can give a single image or video frame a variety of visual effects at once. And the filter can be joined together to form a filter chain, and the filter effect is superimposed to process the image.

The most common classes of the Coreimage framework are:


* Ciimage

Classes that hold image data can be created by uiimage, image files, or pixel data, including unhandled pixel data such as:

-Imagewithcvpixelbuffer:

-Imagewithdata:

Methods, and so on.

You can also use image data classes such as Uiimage,cgimageref and so on.


* Cifilter

Filter class, a class that deals with the details of picture properties in this frame. It operates on all pixels and uses some key-value settings to determine the degree of operation.


* Cicontext

Context classes, such as coregraphics and contexts in CoreData, are used to handle drawing rendering and handling managed objects, and the context of coreimage is also a concrete object for implementing image processing.

It is important to note that when the context is created, we need to set it to be GPU-or CPU-based. (use GPU here)

GPU-based, processing is faster because it leverages the parallel advantages of GPU hardware. But GPU is limited to hardware texture size, and if your program continues to process and save pictures in the background, you need to use the CPU, because GPU processing will be interrupted when the app switches to the background state.

Steps to use:

0. Import Ciimage Pictures

Ciimage *ciimage = [[Ciimage alloc]initwithimage:[uiimage imagenamed:@"test.jpg"]];

1. Create a filter filter

Cifilter *filterone = [Cifilter filterwithname:@"cipixellate"];
[Filterone setvalue:ciimage Forkey:kciinputimagekey];
[Filterone SetDefaults];

Ciimage *outimage = [Filterone Valueforkey:kcioutputimagekey];

2. Use Cicontext to render the image in the filter

Cicontext *context = [Cicontext Contextwithoptions:nil];

Cgimageref cgimage = [Context createcgimage:outimage fromrect:[outimage extent];

3. Exporting pictures

UIImage *showimage = [UIImage imagewithcgimage:cgimage];
Cgimagerelease (Cgimage);

4. Loading pictures

_image.image = ShowImage;

If you want to use the combo filter

To set up the combo filter in step 1, simply change the output of the previous filter to the input of the next filter.

//First Filter
Cifilter *filterone = [Cifilter filterwithname:@"cipixellate"];
[Filterone setvalue:ciimage Forkey:kciinputimagekey];
[Filterone SetDefaults];
Ciimage *outimage = [Filterone Valueforkey:kcioutputimagekey];
//a second filter
Cifilter *filtertwo = [Cifilter filterwithname:@"Cihueadjust"];
[Filtertwo setvalue:outimage Forkey:kciinputimagekey];
[Filtertwo SetDefaults];
[Filtertwo setvalue:@ (1. f) Forkey:kciinputanglekey];
Ciimage *outputimage = [Filtertwo Valueforkey:kcioutputimagekey];

Remember when rendering, step 2, to pass the last output of ciimage

Cicontext *context = [Cicontext Contextwithoptions:nil];

Cgimageref cgimage = [Context createcgimage:outputimage fromrect:[outimage extent];

Ios-coreimage Simple to use

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.