【问题标题】:UIImageView shows the image for first time and then suddenly goes back to the old image in Swift3UIImageView 第一次显示图像然后突然回到 Swift3 中的旧图像
【发布时间】:2018-03-21 22:21:01
【问题描述】:

我正在使用 UIImageView 来更改用户的图像。目前,用户可以选择删除已经存在的图像。为此,我有一个名为“删除照片”的 UIButton,并且在操作中代码编写为:

@IBAction func removeUserPhoto(_ sender: UIButton) {
    self.profilePhoto.image = UIImage (named: "defaultProfilePic")
    self.photoChanged = true
    let imageData: Data =  UIImagePNGRepresentation(profilePhoto.image!)!
    tempProfileImage = imageData
    self.removePhoto.isHidden = true
   }

当我单击删除图像时,图像设置为 defaultProfilePic,但几秒钟后图像视图再次显示用户已删除的旧图像。

注意:尚未调用任何 API 调用。

截图:

点击删除按钮后:

几秒钟后,它又会自动变回旧图像。

为什么会这样?我发现 viewDidLayoutSubviews 调用了两次或更多次。
我正在使用 Xcode 8.3 Beta。

【问题讨论】:

  • 这个图片视图有什么用tempProfileImage
  • 它被存储到用户默认值。这是为了在其他屏幕上快速显示更改的图像。
  • 您没有提供问题的完整描述。您需要描述不同的图像视图,它们是如何在故事板中设置的,并发布显示/隐藏不同图像视图的所有代码。狮子有一个很好的观点。看起来你最好有一个显示当前图像的图像视图。如果是占位符,则显示,如果是自定义图像,则显示。

标签: ios swift image swift3 uiimageview


【解决方案1】:

你应该删除类似的图像,

  yourImageView.image = UIImage.init(named: "your place holder image name")

我的意思是将您的占位符图像分配给您的图像视图!而已!无需拍摄两个图像视图!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多