目标:裁剪UIImage
如何在不丢失scale属性的情况下裁剪UIImage?
我执行以下码(与2.0的scale
属性开始):
let croppedCGImage = originalUIImage.cgImage!.cropping(to: cropRect)
let croppedUIImage = UIImage(cgImage: croppedCGImage!)
此代码的工作,然而,结果是,croppedUIImage
,具有一个不正确的scale
属性1.0。
我试图创建最终的图像时指定scale
:
let croppedUIImage = UIImage(cgImage: croppedCGImage!, scale: 2.0, orientation: .up)
这将产生正确的比例,但它切割size
尺寸一半不正确。
我应该在这里做什么?
(*注:因为我后来保存UIImagePNGRepresentation(_ image: UIImage)
这是由scale
属性影响图像的scale
财产上的UIImage很重要):
我
编辑以下工作。不幸的是,它的速度比CGImage
裁剪功能慢得多。
extension UIImage {
func cropping(to rect: CGRect) -> UIImage {
UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale)
self.draw(in: CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return croppedImage
}
}
你怎么想到这个代码就会突然从矩形的位置和大小的图像?它不适合我 –