iOS 5 | How-To Tutorial on Creating Core Image Applications for iPad, iPhone or iPod Touch

The big difference to Core Image is that it is now available in the iOS 5 SDK. However you cannot create custom filters like you can using the Mac OSX 10.7 SDK version. You can however use a subset of existing filters to enhance images, photos and other graphic media. Third party filters can also be used, thus allowing you to create your own filters for re distribution. The framework provides powerful editing capabilities like enhance hues, vibrance or distortion.

The Core Image process works by making a copy of your original image and applying the changes to the copy. You can stack several filters together to create stunning transformations. When combined, the filters act as one so the effects are applied at the same time in near realtime. Each filter has its own parameters can be applied. You can also programmatically obtain information on which filter was applied to an image.

The Core Image Framework has 8 main classes and the also the Core Image Filters.

CIImage

CIImage represents the image that will undergo transformation. The image can be from a file, pixel data or created with UIImage.

CIFilter

The CIFilter class applies filters to an initial copy of the image, or input CIImage and transforms the copy by applying filters to the image and produces an output CIImage. The filters are key/value pairings that are applied to an image. Several filters can be combined to allow a user to completely transform the input image.

CIColor

The CIColor represents the different color transformations that can be applied to a CIImage. When applying a color, you can use a constant like colorWhite or colorBlue, or you can opt to use RGB values. The colors can be defined as -one to -four environmental dimension whose components different intensities. The color value range fro 0.0 to 1.0 where 0.0 is void of color and 1.0 is opaque.

CIContext

The CIContext is the context where the transformation is being applied using Quartz 2d or Open GL. The CIContext works in conjunction with the other classes in the framework to apply filters to transform the image.

CIDetector

The CIDetector can be used to find specific features in an image.

CIFaceFeature

This class has a narrow focus as it is used to detect specific features in faces in an image. For instance the class can find the eyes and mouth in a face in an image.

CIFeature

This class represent a portion of an image that has be defined with the CIDetector class.

CIVector

The CIVector manges to the vector coordinates on an image. This class is frequently used to pass x, y, z coordinates to the CIFilter class to apply a particular to a certain area of an image.

Sample Core Image App

Here is a small example on how to apply image filtering to photo image that is stored in a file. The app will apply a filter to invert the colors in the photo.

  • To start create a Single View project for the iPad and add the QuartzCore.framework and the CoreImage.framework to the Linked Frameworks and Libraries. Take a look at the screenshot (Figure 1) on how to add frameworks to a project.

Figure 1: Adding the CoreImage Frameworks to the project.
Figure 1: Adding the CoreImage Frameworks to the project.
  • Once the framework is added, drag the file to the Frameworks group. Then open the storyboard and add an UImageView Controller to the View Controller. You may need to resize to fit the screen or if you want to add a label. Figure 2 below is the UI for the sample app.

Figure 2: Storyboard Layout
Figure 2: Storyboard Layout

In this example, a filter will be applied to invert the colors of a supplied image. The best place to start building our application is in the klViewController.h header file. The code for the example is provided in listing 1.

Code Listing 1 - Core Image Example - klViewController Header

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreImage/CoreImage.h>

@interface klViewController : UIViewController

@property(nonatomic, strong) NSString * file;
@property(nonatomic, strong) NSURL * imageFile;
@property(nonatomic, strong) CIImage * myImage;
@property(nonatomic, strong) CIImage * myImageOut;
@property(nonatomic, strong) CIFilter * filters;
@property(nonatomic, strong) CIContext * context;
@property(nonatomic, strong) UIImage * invertedImage;

@property (strong, nonatomic) IBOutlet UIImageView *ImageViewer;

-(void)LoadImage;

@end
  • For this example, we will need to create an instance variable for our file path which will be a NSString. We will use the NSBundle resourceForPath to get the path and filename of the image file that we will transform.
  • Next create an instance variable for the NSUrl which will represent the actual image file. The two CIImage instance methods are for the input and output images. The CIImage instance variable, myImage, will use the imageFile as an input and will apply a filter to a copy of the orignal.
  • Once the filter has been applied, the output CIImage, myImageOut will be assigned the result of the filtering and will passed to a CGImageRef instance variable that will be defined in the implementation file.
  • Finally the transformed image will be assigned to the UIImage and added to the UIImageView IBOutlet.
  • To create the IBOutlet connection, open the storyboard and select the Assistant Editor which will open the header file besides the Storyboard canvas. Perform a control+drag from the UIImageView to the header file. In the popup specify IBOutlet as a connection type and give our UIImageView an identifier like imageViewer. Figure 2 provides a screenshot of the process.

Figure 2: Create an IBOutlet connection for UIImageView
Figure 2: Create an IBOutlet connection for UIImageView

Next you will need to add some code to the loadImage method in the View Controller to get a handle on the image file and create an UIImage object. Notice in the code in listing 2 below that context instance variable is set to nil. This is because we will be using the default values, otherwise we could pass a NSDictionary of CIContext values to evaluate the CIImage that will be transformed by one or more filters. For the filter, we will use the CIColorInvert option but we could have chosen several others filters or for that matter added extra filters to apply a composite filtering. Once the filters are applied, the output is assigned to the myImageOut instance variable using the valueForKey:@"outputImage" option. Then the output image is passed to the CGImageRef which is a bitmap representation of our transformed image which will be added to the invertedImage instance variable. It is the UIImage's job to display image data and can use different sources to to display this data. It is in scope to NSData. Finally our UIImage to added to the UIImageView controller to be rendered on screen.

Code Listing 2 loadImage Implementation

-(void)loadImage{
   
    file =[[NSBundle mainBundle] pathForResource:@[md]me_at_peyto_lake[md] ofType:@[md]jpg[md]];
    imageFile = [NSURL fileURLWithPath:file];
    
    myImage = [CIImage imageWithContentsOfURL:imageFile];
    context = [CIContext contextWithOptions:nil];
    filters = [CIFilter filterWithName:@[md]CIColorInvert[md]
                         keysAndValues:kCIInputImageKey, myImage, nil];
    
    myImageOut =[filters valueForKey:@[md]outputImage[md]];
    CGImageRef imgReference = [context createCGImage:myImageOut fromRect:[myImageOut extent]];
    
    invertedImage = [UIImage imageWithCGImage:imgReference];
    
    [ImageViewer setImage:invertedImage];   
}

You can find a complete list of filters on the Apple web site: https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/uid/TP40004346

This is original file and the version that was transformed with the CIColorInvert filter:

Figure 3: This is the original file which is me at Peyto Lake in Alberta Canada
Figure 3: This is the original file which is me at Peyto Lake in Alberta Canada
Figure 4: This the same photo with the inverted colors.
Figure 4: This the same photo with the inverted colors.

CoreImage is an amazing framework and offers tremendous power and ressources to every developer. I invited you to further explore the framework and experiment with the different filters.

More by this Author


Comments 2 comments

Pranav 3 years ago

It is not worked for me. can you please give us your tutorial link. It will be better helpful.


klanguedoc profile image

klanguedoc 3 years ago from Canada Author

the sample code can be downloaded from www.iosdev101.com/downloads

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    klanguedoc profile image

    Kevin Languedoc (klanguedoc)323 Followers
    144 Articles

    Kevin is Software Developer with 20 years experience designing and building software applications including iOS and Android apps.


    Click to Rate This Article
    working