cropping(to:)
Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
Declaration
func cropping(to rect: CGRect) -> CGImage?Parameters
- rect:
A rectangle specifying the portion of the image to keep.
Return Value
A CGImage object that specifies a subimage of the image. If the rect parameter defines an area that is not in the image, returns NULL.
Discussion
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.
[Image]
cropping(to:) performs the following tasks to create the subimage:
It calls the CGRectIntegral(_:) function 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 theimageparameter.It reads the pixels within the resulting rectangle, treating the first pixel within as the origin of the subimage.
If W and 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 cropped(to:).