stretchCrop()
Distorts an image by stretching or cropping to fit a specified size.
Declaration
class func stretchCrop() -> any CIFilter & CIStretchCropReturn Value
The distorted image.
Discussion
This method applies the stretch crop filter to an image. This effect distorts an image by stretching an image and then applies the crop extent. If the crop value is 0, the filter only uses stretching. If the value is 1, then the filter only uses cropping.
The stretch crop filter uses the following properties:
inputImageAn image with the type CIImage.
centerStretchAmountA
floatrepresenting the amount of stretching of the center of the image as an NSNumber.sizeA CGPoint representing the desired size of the output image.
cropAmountA
floatrepresenting the amount of cropping you apply to achieve the target size as an NSNumber.
The following code creates a filter that results in a smaller image that’s distorted and cropped to be the defined size:
func stretchCrop(inputImage: CIImage) -> CIImage {
let filter = CIFilter.stretchCrop()
filter.inputImage = inputImage
filter.cropAmount = 0.25
filter.centerStretchAmount = 0.25
filter.size = CGPoint(
x: inputImage.extent.width * 2,
y: inputImage.extent.size.height * 0.8
)
return filter.outputImage!
}[Image]