Contents

cropped(to:)

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

Declaration

func cropped(to rect: CGRect) -> CIImage

Parameters

  • rect:

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

Return Value

An image object cropped to the specified rectangle.

Discussion

[Image]

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.

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 CGImage or UIImage, with no additional Core Image application, consider cropping in Core Graphics using the cropping(to:) function to save processing overhead from conversion of images to CIImage. It makes most sense to use cropped(to:) when you already have CIImage in your pipeline.

See Also

Creating an Image by Modifying an Existing Image