iOS5 Core Image filters

Goran on

iOS5-core-image-blog

One of new features in iOS5 is Core Image. It allows you to create amazing image effects in your apps. Core Image comes with several built-in filters, in this tutorial I’ll show you how to use them.

Create new project “Single View Application”, add CoreImage framework to project, add UIImageView to your view with Interface Builder, create IBOutlet in code and connect it to UIImageView. Now we are ready to start writing code, lets write it in viewDidLoad method.

First we’ll create CIImage on which we want to apply effect. Than we’ll define filter we want to apply on image, first filter here will be CIHueAdjust. After we define filter, we’ll set default values to it, and than we’ll set value for filter specific key. CIHueAdjust have key inputAngle since Hue is defined as color wheel. CIImage with applied image effect can be retrieved by accessing outputImage key value. After we create new context in which image will be drawn, we can set this image to our UIImageView.

// CIImage on which we apply effect
CIImage *inputImage = [[CIImage alloc] initWithImage:
    [UIImage imageNamed:@"image.png"]];
// CIFilter CIHueAdjust
CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setDefaults];
[hueAdjust setValue:inputImage forKey:@"inputImage"];
[hueAdjust setValue:[NSNumber numberWithFloat: 2.0f]
    forKey:@"inputAngle"];
// CIImage with effect
CIImage *outputImage = [hueAdjust valueForKey:@"outputImage"];
// define context
CIContext *context = [CIContext contextWithOptions:nil];
// set image to UIImageView
myImageView.image = [UIImage imageWithCGImage:
    [context createCGImage:outputImage
    fromRect:outputImage.extent]];
// release stuff
[inputImage release];

On image below you can see original image and resulting image after we applied CIFilter named CIHueAdjust:

I’ll show you how to use few more filters: CIGammaAdjust, CIExposureAdjust and CIColorMonochrome.

CIGammaAdjust filter is used to adjust midtone brightness.

// CIGammaAdjust
CIFilter *gammaAdjust =
    [CIFilter filterWithName:@"CIGammaAdjust"];
[gammaAdjust setDefaults];
[gammaAdjust setValue: inputImage forKey: @"inputImage"];
[gammaAdjust setValue: [NSNumber numberWithFloat: 3.0f]
    forKey: @"inputPower"];

CIExposureAdjust filter adjusts the exposure setting for an image.

// CIExposureAdjust
CIFilter *expAdjust =
    [CIFilter filterWithName:@"CIExposureAdjust"];
[expAdjust setDefaults];
[expAdjust setValue: inputImage forKey: @"inputImage"];
[expAdjust setValue: [NSNumber numberWithFloat: 2.0f]
    forKey: @"inputEV"];

CIColorMonochrome filter is used to remap colors so they fall within shades of a single color you define.

// CIColorMonochrome
CIFilter *colorMonochrome =
    [CIFilter filterWithName:@"CIColorMonochrome"];
[colorMonochrome setDefaults];
[colorMonochrome setValue: inputImage
    forKey: @"inputImage"];
[colorMonochrome setValue:
    [CIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f]
    forKey: @"inputColor"];

Results for those three filters looks like this:

There are some more advanced filters than those few shown here, I guess you have idea how to use Core Image now, so feel free to check Core Image Filter Reference for list of filters and list of parameters they use.

Thanks for reading!

Leave Your Comment

You’re more than welcome to leave your own comment!

Just please, pretty please: don’t spam. Thank you :)