【问题标题】:UIImage is not updated after image view is altered更改图像视图后 UIImage 未更新
【发布时间】:2021-03-03 09:48:19
【问题描述】:

当我想编辑我的 UIImageView(添加边框、背景颜色等)并从中获取最终图像时,我遇到了一个非常简单但非常奇怪的问题。它给了我初始化的原始图像,没有改变。

我基本上使用来自this 库的SOXPanRotateZoomImageView.swift,它将手势应用于我的图像以进行旋转、缩放等。

下面是我的代码:

class ImageEditorViewController: UIViewController {

  public init(rawImage: UIImage?) {
    self.image = rawImage
    
    super.init(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self)))
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    let editedImageView = SOXPanRotateZoomImageView(image: self.image)
    self.panView.addSubview(editedImageView)
    editedImageView.backgroundColor = .red
    editedImageView.layer.cornerRadius = 20

    let pickedImage = editedImageView.image // Here is original image, without altering..
  }
}

editedImageView 中我可以看到更改,但是当我想访问它的图像时,它是原始图像。之前的一些提问推荐setNeedsDisplay(),我申请了editedImageView.setNeedsDisplay();它没有帮助。如果我需要提供更多信息或更清楚,请告诉我。感谢您的帮助。

【问题讨论】:

    标签: ios swift uiimageview uiimage uigesturerecognizer


    【解决方案1】:

    注意SOXPanRotateZoomImageView.swift 执行旋转、平移和捏合到UIImageView 而不是UIImage。 换句话说,它改变的是 ui 容器,而不是图像实例。

    因此,当您尝试获取像 let pickedImage = editedImageView.image 这样的图像时,您会收到原始图像。

    如果你想执行和保存对图像的更改,你必须寻找其他库或自己编写。

    【讨论】:

    • 感谢@Mikhail Vasilev 的回复,正如您在SOXPanRotateZoomImageView.swift 中看到的那样,它只是扩展到 UIImageView 并在图像视图上添加手势。它既不改变图像视图也不改变图像的边框、背景颜色等。这意味着如果我使用普通的 UIImageView,结果不会改变。我可能需要找到一种方法来捕获 UIImageView 并继续而不是 UIImage。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多