我有一张完整尺寸为 1800 x 2400 像素的图像。它以 450 x 600 的尺寸显示在 UIImageView 中。
我已将 UIPinchGestureRecognizer
和 UIRotationGestureRecognizer
附加到 UIImageView,它允许用户缩放和旋转图像,效果很好。
我现在需要以原始尺寸 (1800 x 2400) 导出图像并进行旋转和变换,但我正在努力使其工作。
到目前为止我的代码:
if let imageView = imageView, let image = imageView.image {
UIGraphicsBeginImageContext(image.size)
let context = UIGraphicsGetCurrentContext();
context?.translateBy(x: 0, y: image.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
context?.rotate(by: -imageView.transform.b)
context?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
translatedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
}
通过上述内容,旋转是正确的,但图像偏离中心,我不知道如何包括缩放。
图形是我在开发中的弱点,我已经为此花费了几个小时但没有成功。
最佳答案
切勿缩放图像本身,因为这非常耗时,而且不是推荐的方法。
使用scrollView进行缩放,您无需考虑图像大小。将大尺寸图像放入xcassets中。 ScrollView 具有用于缩放的委托(delegate)功能。它也有一些漂亮的属性,就像这样。
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 6.0;
所以最好将你的精力转移到 UIScrollView 上,你会发现你不必为此编写那么多代码。
关于ios - 以完整尺寸保存 UIImage,包括 UIImageView 的变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44304678/