this is almost the answer Alexburtnik
but just mention that UIImage.size is a logical size (in “dots”)
however, CGImage.cropping () used the actual measurement (in pixels)
therefore, if you use an image with @ 2x or @ 3x modifiers, you will find the cropping is actually half or a third of the expected result.
so when you crop, you might first consider multiplying the rectangle with the "scale" property of the image, for example the following:
func cropImage(image:UIImage, toRect rect:CGRect) -> UIImage? { var rect = rect rect.size.width = rect.width * image.scale rect.size.height = rect.height * image.scale guard let imageRef = image.cgImage?.cropping(to: rect) else { return nil } let croppedImage = UIImage(cgImage:imageRef) return croppedImage }
source share