ios - 以完整尺寸保存 UIImage,包括 UIImageView 的变换

标签 ios swift core-graphics

我有一张完整尺寸为 1800 x 2400 像素的图像。它以 450 x 600 的尺寸显示在 UIImageView 中。

我已将 UIPinchGestureRecognizerUIRotationGestureRecognizer 附加到 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/

相关文章:

ios - 从使用 Swift 或 Objective C 中的密码短语编码的 RSA 私钥字符串中检索 SecKey

ios - 无法单击 UIScrollView 内的组件

ios - 尝试在 Swift 中推送 View Controller 时崩溃

ios - SwiftyJSON读取本地文件

iphone - 如何在 Core Graphics 中生成 RGBA 图像?

ios - 使用 swrevealviewcontroller 从 uitableviewcell 导航到多个 uinavigationcontroller

ios - TableView 单元格操作

swift - TableView 在 IB 中设计时显示内容,但在我以编程方式编写时不显示内容,我做错了什么

objective-c - CAShapeLayer 作为 CALayer 的掩码 : rounded corners are stretched

iphone - 如何在 UIView 下绘制阴影?