【发布时间】:2020-12-25 11:04:34
【问题描述】:
我正在为我的应用程序制作扫描仪,但是当我裁剪图像时,它总是错误地裁剪它。
图像上的裁剪矩形-
裁剪后的图像-
我的矩形裁剪代码...
func croppedImage(rect: CGRect) -> UIImage {
print("rect: \(rect)")
UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()! as CGContext
let drawRect : CGRect = CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height)
context.clip(to: CGRect(x:0, y:0, width: rect.size.width, height: rect.size.height))
self.draw(in: drawRect)
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return croppedImage
}
我尝试了无数不同的代码 sn-ps 来正确裁剪图像,它们都得到了相同的结果。
docVC.croppedImage = pdfImage.croppedImage(rect: croppingRect)
docVC 是一个视图控制器,我将图像传递到 pdfImage 是您在第一张图片中看到的整个图像 croppingRect 是应该没问题的矩形
我真的需要这方面的帮助,过去 2 小时我一直在尝试只裁剪一张图像。
【问题讨论】:
标签: swift xcode uiimage crop swift5