【问题标题】:swift 3.0 check UIImageView is nil or notswift 3.0 检查 UIImageView 是否为零
【发布时间】:2017-07-31 06:34:49
【问题描述】:

我在 viewcontroller1 的 UIImageView 中使用占位符图像“a.png”。当用户从他的手机图库中选择任何图像时,所选图像将显示在 UIImageview 中。现在,如果用户从 viewcontroller1 转到 viewcontroller2,我必须检查我的图像视图是否为空。为此,我正在写:

if myimageview.image == nil
    { // show alert

... }

但是,代码不起作用,因为 imageview 已经包含一个占位符图像。

谁能建议我如何验证我的imageview?

【问题讨论】:

  • 您找到解决方案了吗?

标签: swift3 uiimageview uiimage


【解决方案1】:

您可以通过您的placeholder 图片查看。

let img = UIImage(named: "a.png")
//Now check if the img has changed or not:
if imageView1?.image != img {
//Do something
}

【讨论】:

    【解决方案2】:

    由于您已将占位符图像分配给 imageView.image 属性,因此最好保留任何布尔检查以了解用户是否已更新图像。将此布尔值保持为 false,因为用户从图库或相机中选择图像使其成为 true。 在导航到 viewcontroller2 检查布尔变量的值。

    【讨论】:

      【解决方案3】:

      在模型中声明一个布尔属性

      var hasCustomImage = false
      

      选择自定义图像后,将属性设置为true 并检查

      【讨论】:

        【解决方案4】:

        Swift 3.0 代码

        第一个解决方案是..

         imgview.image= nil
        

        第二个灵魂是..

          imgview.image= UIImage.init(named: "")
        

        【讨论】:

        • OP 已经尝试了问题中的第一个解决方案。而且第二种解决方案不需要init
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 2017-01-04
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 2012-05-02
        • 2011-04-13
        相关资源
        最近更新 更多