【问题标题】:Swift display image UIImageviewSwift 显示图像 UIImageview
【发布时间】:2014-11-29 08:23:01
【问题描述】:

如何在 UIImageview 中显示图像?然后根据按下的按钮更改图像。

我是新手,请放轻松。

【问题讨论】:

    标签: swift uiimageview


    【解决方案1】:

    为此,首先确保视图控制器中有可用的图像(通过在情节提要上的图像和视图控制器中创建关联)。

    我们假设它叫做 imageView。

    以编程方式,您可以说:

      imageView.image = UIImage(named: ("nameofasset"))
    

    我实际上最终做的是创建一个静态函数,负责确定要返回的图像:

    class func GetImage(someValueWhichDrivesLogic: Int)->UIImage
    {
        if (someValueWhichDrivesLogic == 1)
        { 
          assetName = "imageone" //note you don't add .png, you just give it the same name you have in your standard images folder in XCode6
        }
        else if (someValueWhichDrivesLogic == 2)
        {
          assetName = "imagetwo" //again, this is the actual name of my image
        }
    
        //Return an image constructed from what existed in my images folder based on logic above
        return UIImage(named: (assetName))
    
    }
    

    现在上面的函数已经创建好了,你可以这样做:

     imageView.image = GetImage(1) 
     imageView.image = GetImage(2) 
    

    这实际上会将图像动态分配给图像。你可以这样做,或者如果你只是在做一些简单的事情,比如将它放入一个按钮点击中,你可以采用我上面指定的更简单的方法。

    谢谢,如果有任何问题,请告诉我!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多