IOS coreimage/image Blur (i)

Source: Internet
Author: User

Coreimage is introduced from iOS5.0, and its powerful not by Marvel.

Whether it is sweep, generate two-dimensional code, face recognition, image compression, filters and other processing, should have.



image Blur is supported from 6.0 onwards, we look at a simple example below.


First, an object is introduced: Ciimage is not UIImage.

Ciimage is a picture object in the Coreimage.

The common way to create is

<span style= "FONT-SIZE:14PX;" >+ (Ciimage *) Imagewithcontentsofurl: (Nsurl *) url;+ (Ciimage *) Imagewithcontentsofurl: (nsurl *) URL options: ( Nsdictionary *) d;+ (Ciimage *) Imagewithdata: (NSData *) data;+ (Ciimage *) Imagewithdata: (NSData *) Data options: ( Nsdictionary *) d;</span>

We can create a Ciimage object first

1. First you import 1. JPG files into the project. Then introduce the resource path

<span style= "FONT-SIZE:14PX;" >    ciimage *beginimage = [ciimage imagewithcontentsofurl:[[nsbundle mainbundle] urlforresource:@ "1.jpg" Withextension:nil]];</span>
2. Create a Cicontext context

<span style= "FONT-SIZE:14PX;" >    Cicontext *context = [Cicontext contextwithoptions:nil];</span>
3. Create a filter

Cifilter is a filter object that needs to pass in a filter name. (There are nearly hundreds of filter names). Kciinputimagekey refers to the key of the input image

Cifilter *filter = [Cifilter filterwithname:@ "Cigaussianblur" keysandvalues:kciinputimagekey,beginimage, nil];

4. Get the picture of the output

<span style= "FONT-SIZE:14PX;" >    Ciimage *outputimage = [Filter outputimage];</span>


5. Create a piece of cgimageref by context

<span style= "FONT-SIZE:14PX;" >cgimageref ref = [Context Createcgimage:outputimage fromrect:[outputimage extent]];</span>

6. Convert ref to UIImage object

<span style= "FONT-SIZE:14PX;" >    UIImage *newimage = [UIImage imagewithcgimage:ref];</span>

7. Freeing up memory

<span style= "FONT-SIZE:14PX;" >cgimagerelease (ref);</span>


If you create more cicontext, you will consume memory very much.

So, in general I recommend that you refer to the Cicontext\cifiter object as a property.


That's all you can do.

Suppose we need to have a slider to set the blur amount of the picture.

----slide to change the transparency of the picture

Slider To change the transparency when changing value

<span style= "FONT-SIZE:14PX;" >[self.slider addtarget:self Action: @selector (Sliderchangevalue:) forcontrolevents:uicontroleventvaluechanged] ;</span>


<span style= "FONT-SIZE:14PX;" >-(void) Sliderchangevalue: (UISlider *) slider{        [Self.filter setValue:self.beginImage Forkeypath: Kciinputimagekey];    [Self.filter setvalue:[nsnumber NumberWithFloat:slider.value *] forkey:@ "Inputradius"];        Get filtered picture    ciimage *outputimage = [Self.filter outputimage];        Convert picture    cgimageref cgimg = [Self.context createcgimage:outputimage fromrect:[outputimage extent];    UIImage *newimg = [UIImage imagewithcgimage:cgimg];    Show Image    [Self.imagev setimage:newimg];    Release C object    cgimagerelease (cgimg);} </span>


That's all you can do.



IOS coreimage/image Blur (i)

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.