Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
- iOS 2.0+
- macOS 10.4+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
- Core Graphics
A rectangle specifying the portion of the image to keep.
CGImage object that specifies a subimage of the image. If the
rect parameter defines an area that is not in the image, returns
Cropping removes content around the designated rectangle; it cuts out the desired area of the input image and returns an image of the cropped size.
cropping(to:) performs the following tasks to create the subimage:
It calls the
integralfunction to adjust the
rectparameter to integral bounds.
It intersects the
rectwith a rectangle whose origin is
(0,0)and size is equal to the size of the image specified by the
It reads the pixels within the resulting rectangle, treating the first pixel within as the origin of the subimage.
H are the width and height of image, respectively, then the point
(0,0) corresponds to the first pixel of the image data. The point
(W–1, 0) is the last pixel of the first row of the image data, while
(0, H–1) is the first pixel of the last row of the image data and
(W–1, H–1) is the last pixel of the last row of the image data.
The resulting image retains a reference to the original image, which means you may release the original image after calling this function. In Swift, you do not need to release the original image reference explicitly.
If you already use
CIImage, or if you are post-processing images as
CIImage data in Core Image, such as chaining together multiple filters to the cropped result, it may be more efficient to crop
CIImage directly in the Core Image framework using the
CICrop filter; in this case, use the convenience function