CIImage Class Reference

Inherits from
Conforms to
Framework
Library/Frameworks/CoreImage.framework
Availability
Available in iOS 5.0 and later.
Companion guide
Declared in
CIImage.h
Related sample code

Overview

The CIImage class represents an image. Core Image images are immutable. You use CIImage objects in conjunction with other Core Image classes—such as CIFilter, CIContext, CIVector, and CIColor—to take advantage of the built-in Core Image filters when processing images. You can create CIImage objects with data supplied from a variety of sources, including Quartz 2D images, Core Video image buffers (CVImageBufferRef), URL-based objects, and NSData objects.

Although a CIImage object has image data associated with it, it is not an image. You can think of a CIImage object as an image “recipe.” A CIImage object has all the information necessary to produce an image, but Core Image doesn’t actually render an image until it is told to do so. This “lazy evaluation” method allows Core Image to operate as efficiently as possible.

CIContext and CIImage objects are immutable, which means each can be shared safely among threads. Multiple threads can use the same GPU or CPU CIContext object to render CIImage objects. However, this is not the case for CIFilter objects, which are mutable. A CIFilter object cannot be shared safely among threads. If you app is multithreaded, each thread must create its own CIFilter objects. Otherwise, your app could behave unexpectedly.

Core Image defines methods for creating and initializing images. Additional methods that support drawing and initializing an image with an NSBitmapImageRep object are defined in CIImage Additions Reference.

Core Image also provides autoadjustment methods that analyze an image for common deficiencies and return a set of filters to correct those deficiencies. The filters are preset with values for improving image quality by altering values for skin tones, saturation, contrast, and shadows and for removing red-eye or other artifacts caused by flash. (See “Getting Autoadjustment Filters”.)

For a discussion of all the methods you can use to create CIImage objects on iOS and OS X, see Core Image Programming Guide.

Tasks

Creating an Image

Creating an Image by Modifying an Existing Image

Initializing an Image

Getting Image Information

Getting Autoadjustment Filters

Working with Filter Regions of Interest

Class Methods

emptyImage

Creates and returns an empty image object.

+ (CIImage *)emptyImage
Return Value

An image object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithBitmapData:bytesPerRow:size:format:colorSpace:

Creates and returns an image object from bitmap data.

+ (CIImage *)imageWithBitmapData:(NSData *)d bytesPerRow:(size_t)bpr size:(CGSize)size format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs
Parameters
d

The bitmap data for the image. This data must be premultiplied.

bpr

The number of bytes per row.

size

The dimensions of the image.

f

The format and size of each pixel. You must supply a pixel format constant. See “Pixel Formats”.

cs

The color space that the image is defined in. If this value is nil, the image is not color matched. Pass nil for images that don’t contain color data (such as elevation maps, normal vector maps, and sampled function tables).

Return Value

An image object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithCGImage:

Creates and returns an image object from a Quartz 2D image.

+ (CIImage *)imageWithCGImage:(CGImageRef)image
Parameters
image

A Quartz 2D image (CGImageRef) object. For more information, see Quartz 2D Programming Guide and CGImage Reference.

Return Value

An image object initialized with the contents of the Quartz 2D image.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
CIImage.h

imageWithCGImage:options:

Creates and returns an image object from a Quartz 2D image using the specified options.

+ (CIImage *)imageWithCGImage:(CGImageRef)image options:(NSDictionary *)d
Parameters
image

A Quartz 2D image (CGImageRef) object. For more information, see Quartz 2D Programming Guide and CGImage Reference.

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

An image object initialized with the contents of the Quartz 2D image and the specified options.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithColor:

Creates and returns an image of infinite extent that is initialized the specified color.

+ (CIImage *)imageWithColor:(CIColor *)color
Parameters
color

A color object.

Return Value

The image object initialized with the color represented by the CIColor object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithContentsOfURL:

Creates and returns an image object from the contents of a file.

+ (CIImage *)imageWithContentsOfURL:(NSURL *)url
Parameters
url

The location of the file.

Return Value

An image object initialized with the contents of the file.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithContentsOfURL:options:

Creates and returns an image object from the contents of a file, using the specified options.

+ (CIImage *)imageWithContentsOfURL:(NSURL *)url options:(NSDictionary *)d
Parameters
url

The location of the file.

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

An image object initialized with the contents of the file and set up with the specified options.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithCVPixelBuffer:

Creates and returns an image object from the contents of CVPixelBuffer object.

+ (CIImage *)imageWithCVPixelBuffer:(CVPixelBufferRef)buffer
Parameters
buffer

A CVPixelBuffer object.

Return Value

An image object initialized with the contents of the image buffer object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithCVPixelBuffer:options:

Creates and returns an image object from the contents of CVPixelBuffer object, using the specified options.

+ (CIImage *)imageWithCVPixelBuffer:(CVPixelBufferRef)buffer options:(NSDictionary *)dict
Parameters
buffer

A CVPixelBuffer object.

dict

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

An image object initialized with the contents of the image buffer object and set up with the specified options.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithData:

Creates and returns an image object initialized with the supplied image data.

+ (CIImage *)imageWithData:(NSData *)data
Parameters
data

The data object that holds the contents of an image file (such as TIFF, GIF, JPG, or whatever else the system supports). The image data must be premultiplied.

Return Value

An image object initialized with the supplied data, or nil if the method cannot create an image representation from the contents of the supplied data object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithData:options:

Creates and returns an image object initialized with the supplied image data, using the specified options.

+ (CIImage *)imageWithData:(NSData *)data options:(NSDictionary *)d
Parameters
data

A pointer to the image data. The data must be premultiplied

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

An image object initialized with the supplied data and set up with the specified options.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageWithTexture:size:flipped:colorSpace:

Creates and returns an image object initialized with data supplied by an OpenGL texture.

+ (CIImage *)imageWithTexture:(unsigned int)name size:(CGSize)size flipped:(BOOL)flag colorSpace:(CGColorSpaceRef)cs
Parameters
name

An OpenGL texture. Because CIImage objects are immutable, the texture must remain unchanged for the life of the image object. See the discussion for more information.

size

The dimensions of the texture.

flag

YES to have Core Image flip the coordinates of the texture vertically to convert between OpenGL and Core Image coordinate systems.

cs

The color space that the image is defined in. If the colorSpace value is nil, the image is not color matched. Pass nil for images that don’t contain color data (such as elevation maps, normal vector maps, and sampled function tables).

Return Value

An image object initialized with the texture data.

Discussion

When using a texture to create a CIImage object, the texture must be valid in the Core Image context (CIContext) that you draw the CIImage object into. This means that one of the following must be true:

  • The texture must be created using the CGLContext object that the CIContext is based on.

  • The context that the texture was created in must be shared with the CGLContext that the CIContext is based on.

Note that textures do not have a retain and release mechanism. This means that your application must make sure that the texture exists for the life cycle of the image. When you no longer need the image, you can delete the texture.

Core Image ignores the texture filtering and wrap modes (GL_TEXTURE_FILTER and GL_TEXTURE_WRAP) that you set through OpenGL. The filter and wrap modes are overridden by what the CISampler object specifies when you apply a filter to the CIImage object.

Availability
  • Available in iOS 6.0 and later.
Declared In
CIImage.h

Instance Methods

autoAdjustmentFilters

Returns all possible automatically selected and configured filters for adjusting the image.

- (NSArray *)autoAdjustmentFilters
Return Value

An array of CIFilter instances preconfigured for correcting deficiencies in the supplied image.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

autoAdjustmentFiltersWithOptions:

Returns a subset of automatically selected and configured filters for adjusting the image.

- (NSArray *)autoAdjustmentFiltersWithOptions:(NSDictionary *)dict
Parameters
options

You can control which filters are returned by supplying one or more of the keys described in “Autoadjustment Keys.”

The options dictionary can also contain a CIDetectorImageOrientation key. Because some autoadjustment filters rely on face detection, you should specify an image orientation if you want to enable these filters for an image containing face whose orientation does not match that of the image.

Return Value

An array of CIFilter instances preconfigured for correcting deficiencies in the supplied image.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

extent

Returns a rectangle that specifies the extent of the image.

- (CGRect)extent
Return Value

A rectangle that specifies the extent of the image in working space coordinates.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
CIImage.h

imageByApplyingTransform:

Returns a new image that represents the original image after applying an affine transform.

- (CIImage *)imageByApplyingTransform:(CGAffineTransform)matrix
Parameters
matrix

An affine transform.

Return Value

The transformed image object.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

imageByCroppingToRect:

Returns a new image that represents the original image after cropping to a rectangle.

- (CIImage *)imageByCroppingToRect:(CGRect)r
Return Value

An image object cropped to the specified rectangle.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithBitmapData:bytesPerRow:size:format:colorSpace:

Initializes an image object with bitmap data.

- (id)initWithBitmapData:(NSData *)d bytesPerRow:(size_t)bpr size:(CGSize)size format:(CIFormat)f colorSpace:(CGColorSpaceRef)c
Parameters
d

The bitmap data to use for the image. The data you supply must be premultiplied.

bpr

The number of bytes per row.

size

The size of the image data.

f

A pixel format constant. See “Pixel Formats”.

c

The color space that the image is defined in and must be a Quartz 2D color space (CGColorSpaceRef). Pass nil for images that don’t contain color data (such as elevation maps, normal vector maps, and sampled function tables).

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithCGImage:

Initializes an image object with a Quartz 2D image.

- (id)initWithCGImage:(CGImageRef)image
Parameters
image

A Quartz 2D image (CGImageRef) object. For more information, see Quartz 2D Programming Guide and CGImage Reference.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithCGImage:options:

Initializes an image object with a Quartz 2D image, using the specified options.

- (id)initWithCGImage:(CGImageRef)image options:(NSDictionary *)d
Parameters
image

A Quartz 2D image (CGImageRef) object. For more information, see Quartz 2D Programming Guide and CGImage Reference.

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
CIImage.h

initWithColor:

Initializes an image with the specified color.

- (id)initWithColor:(CIColor *)color
Parameters
color

A color object.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithContentsOfURL:

Initializes an image object by reading an image from a URL.

- (id)initWithContentsOfURL:(NSURL *)url
Parameters
url

The location of the image file to read.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithContentsOfURL:options:

Initializes an image object by reading an image from a URL, using the specified options.

- (id)initWithContentsOfURL:(NSURL *)url options:(NSDictionary *)d
Parameters
url

The location of the image file to read.

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithCVPixelBuffer:

Initializes an image object from the contents of CVPixelBuffer object.

- (id)initWithCVPixelBuffer:(CVPixelBufferRef)buffer
Parameters
buffer

A CVPixelBuffer object.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithCVPixelBuffer:options:

Initializes an image object from the contents of CVPixelBuffer object using the specified options.

- (id)initWithCVPixelBuffer:(CVPixelBufferRef)buffer options:(NSDictionary *)dict
Parameters
buffer

A CVPixelBuffer object.

dict

A dictionary that contains options for creating an image object. (See “Image Dictionary Keys”.) The pixel format is supplied by the CVPixelBuffer object.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
CIImage.h

initWithData:

Initializes an image object with the supplied image data.

- (id)initWithData:(NSData *)data
Parameters
data

The image data. The data you supply must be premultiplied.

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithData:options:

Initializes an image object with the supplied image data, using the specified options.

- (id)initWithData:(NSData *)data options:(NSDictionary *)d
Parameters
data

The image data. The data you supply must be premultiplied.

d

A dictionary specifying image options. (See “Image Dictionary Keys”.)

Return Value

The initialized image object or nil if the object could not be initialized.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

initWithTexture:size:flipped:colorSpace:

Initializes an image object with data supplied by an OpenGL texture.

- (id)initWithTexture:(unsigned int)name size:(CGSize)size flipped:(BOOL)flag colorSpace:(CGColorSpaceRef)cs
Parameters
name

An OpenGL texture. Because CIImage objects are immutable, the texture must remain unchanged for the life of the image object. See the discussion for more information.

size

The dimensions of the texture.

flag

YES to have Core Image flip the coordinates of the texture vertically to convert between OpenGL and Core Image coordinate systems.

cs

The color space that the image is defined in. This must be a Quartz color space (CGColorSpaceRef). If the colorSpace value is nil, the image is not color matched. Pass nil for images that don’t contain color data (such as elevation maps, normal vector maps, and sampled function tables).

Return Value

The initialized image object or nil if the object could not be initialized.

Discussion

When using a texture to create a CIImage object, the texture must be valid in the Core Image context (CIContext) that you draw the CIImage object into. This means that one of the following must be true:

  • The texture must be created using the CGLContext object that the CIContext is based on.

  • The context that the texture was created in must be shared with the CGLContext that the CIContextis based on.

Note that textures do not have a retain and release mechanism. This means that your application must make sure that the texture exists for the life cycle of the image. When you no longer need the image, you can delete the texture.

Core Image ignores the texture filtering and wrap modes (GL_TEXTURE_FILTER and GL_TEXTURE_WRAP) that you set through OpenGL. The filter and wrap modes are overridden by what the CISampler object specifies when you apply a filter to the CIImage object.

Availability
  • Available in iOS 6.0 and later.
Declared In
CIImage.h

properties

Returns a dictionary containing image metadata.

- (NSDictionary *)properties
Return Value

An NSDictionary object containing image metadata.

Discussion

If the CIImage object is the output of a filter (or filter chain), this method returns the metadata from the filter’s original input image.

Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

regionOfInterestForImage:inRect:

Returns the region of interest for the filter chain that generates the image.

- (CGRect)regionOfInterestForImage:(CIImage *)im inRect:(CGRect)r
Parameters
im

Another image that is part of the filter chain that generates the image.

r

A rectangle in the image’s coordinate space.

Return Value

A rectangle in the coordinate space of the input image (the im parameter).

Discussion

The region of interest is the rectangle containing pixel data in a source image (the im parameter) necessary to produce a corresponding rectangle in the output image. If the image is not the output of a filter (or of a chain or graph of several CIFilter objects), or the image in the im parameter is not an input to that filter, the rectangle returned is the same as that in the r parameter.

For example,

  • If the image is the output of a filter that doubles the size of its input image, the rectangle returned will be half the size of that in the r parameter. (Upscaling causes every pixel in the input image to correspond to multiple pixels in the output image.)

  • If the image is the output of a blur filter, the rectangle returned will be slightly larger than that in the r parameter. (In a blur filter, each pixel in the output image is produced using information from the corresponding pixel and those immediately surrounding it in the input image.)

Availability
  • Available in iOS 6.0 and later.
Declared In
CIImage.h

Constants

CIFormat

A data type used for specifying image pixel formats.

typedef int CIFormat;
Availability
  • Available in iOS 5.0 and later.
Declared In
CIImage.h

Pixel Formats

Image data pixel formats.

extern CIFormat kCIFormatARGB8;
extern CIFormat kCIFormatBGRA8;
extern CIFormat kCIFormatRGBA8;
Constants
kCIFormatARGB8

A 32 bit-per-pixel, fixed-point pixel format in which the alpha value precedes the red, green and blue color components.

Available in iOS 6.0 and later.

Declared in CIImage.h.

kCIFormatBGRA8

A 32 bit-per-pixel, fixed-point pixel format in which the blue, green, and red color components precede the alpha value.

Available in iOS 5.0 and later.

Declared in CIImage.h.

kCIFormatRGBA8

A 32 bit-per-pixel, fixed-point pixel format in which the red, green, and blue color components precede the alpha value.

Available in iOS 5.0 and later.

Declared in CIImage.h.

Declared In
CIImage.h

Image Dictionary Keys

Constants used as keys in the options dictionary when initializing an image.

extern NSString *kCIImageColorSpace;
extern NSString *kCIImageProperties;
Constants
kCIImageColorSpace

The key for a color space. The value you supply for this dictionary key must be a CGColorSpaceRef data type. For more information on this data type see CGColorSpace Reference. Typically you use this option when you need to load an elevation, mask, normal vector, or RAW sensor data directly from a file without color correcting it. This constant specifies to override Core Image, which, by default, assumes that data is in GenericRGB.

Available in iOS 5.0 and later.

Declared in CIImage.h.

kCIImageProperties

The key for image metadata properties. If a value for this key isn’t supplied, the image’s colorSpace dictionary are populated automatically by calling CGImageSourceCopyPropertiesAtIndex. To ensure that an image has no metadata properties, set the value of this key to [NSNull null].

Available in iOS 5.0 and later.

Declared in CIImage.h.

Declared In
CIImage.h

Autoadjustment Keys

Constants used as keys in the options dictionary for the autoAdjustmentFiltersWithOptions: method.

NSString *kCIImageAutoAdjustEnhance;
NSString *kCIImageAutoAdjustRedEye;
NSString *kCIImageAutoAdjustFeatures;
Constants
kCIImageAutoAdjustEnhance

A key used to specify whether to return enhancement filters.

The value associated with this key is a CFBoolean value. Supply false to indicate not to return enhancement filters. If you don’t specify this option, Core Image assumes its value is true.

Available in iOS 5.0 and later.

Declared in CIImage.h.

kCIImageAutoAdjustRedEye

A key used to specify whether to return a red eye filter.

The value associated with this key is a CFBoolean value. Supply false to indicate not to return a red eye filter. If you don’t specify this option, Core Image assumes its value is true.

Available in iOS 5.0 and later.

Declared in CIImage.h.

kCIImageAutoAdjustFeatures

A key used to specify an array of features that you want to apply enhancement and red eye filters to.

The associated value is an array of CIFeature objects. If you don’t supply an array, the Core Image searches for features using the CIDetector class.

Available in iOS 5.0 and later.

Declared in CIImage.h.