Instance Method

imageByCroppingToRect:

Returns a new image with a cropped portion of the original image.

Declaration

- (CIImage *)imageByCroppingToRect:(CGRect)rect;

Parameters

rect

The rectangle, in image coordinates, to which to crop the image.

Return Value

An image object cropped to the specified rectangle.

Figure 1

Cropping an image

Butterfly photo with background cropped out

Discussion

Due to Core Image's coordinate system mismatch with UIKit, this filtering approach may yield unexpected results when displayed in a UIImageView with contentMode. Be sure to back it with a CGImage so that it handles contentMode properly.

Listing 1

Backing a CIImage with a CGImageRef to preserve contentMode

CIContext* context = [CIContext context];
CGImageRef cgCroppedImage = [context createCGImage:ciCroppedImage fromRect:ciCroppedImage.extent];
UIImage* croppedImage = [UIImage imageWithCGImage:cgCroppedImage];
CGImageRelease(cgCroppedImage);

If you are displaying or processing your image primarily as a CGImageRef or UIImage, with no additional Core Image application, consider cropping in Core Graphics using the CGImageCreateWithImageInRect function to save processing overhead from conversion of images to CIImage. It makes most sense to use imageByCroppingToRect: when you already have CIImage in your pipeline.

See Also

Creating an Image by Modifying an Existing Image

- imageByApplyingFilter:withInputParameters:

Returns a new image created by applying a filter to the original image with the specified name and parameters.

- imageByApplyingFilter:

Applies the filter to an image and returns the output.

- imageByApplyingTransform:

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

- imageByApplyingOrientation:

Returns a new image created by transforming the original image to the specified EXIF orientation.

- imageByClampingToExtent

Returns a new image created by making the pixel colors along its edges extend infinitely in all directions.

- imageByClampingToRect:

Returns a new image created by cropping to a specified area, then making the pixel colors along the edges of the cropped image extend infinitely in all directions.

- imageByCompositingOverImage:

Returns a new image created by compositing the original image over the specified destination image.

- imageByColorMatchingColorSpaceToWorkingSpace:

Returns a new image created by color matching from the specified color space to the context’s working color space.

- imageByColorMatchingWorkingSpaceToColorSpace:

Returns a new image created by color matching from the context’s working color space to the specified color space.

- imageByPremultiplyingAlpha

Returns a new image created by multiplying the image’s RGB values by its alpha values.

- imageByUnpremultiplyingAlpha

Returns a new image created by dividing the image’s RGB values by its alpha values.

- imageBySettingAlphaOneInExtent:

Returns a new image created by setting all alpha values to 1.0 within the specified rectangle and to 0.0 outside of that area.

- imageByApplyingGaussianBlurWithSigma:

Returns a new image created by applying a Gaussian Blur filter to the image.

- imageBySettingProperties:

Returns a new image created by adding the specified metadata properties to the image.

- imageByInsertingIntermediate

Returns a new image created by inserting an intermediate.

- imageByInsertingIntermediate:

Returns a new image created by inserting a cacheable intermediate.