加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift显示图像UIImageview

发布时间:2020-12-14 04:47:30 所属栏目:百科 来源:网络整理
导读:如何在UI Imageview中显示图像?然后根据按下的按钮更改图像. 我很新,请放轻松. 解决方法 要做到这一点,首先要确保在viewcontroller中有可用的图像(通过在storyboard上和viewcontroller中创建图像之间的关联). 让我们假装它叫做imageView. 以编程方式,您可以
如何在UI Imageview中显示图像?然后根据按下的按钮更改图像.

我很新,请放轻松.

解决方法

要做到这一点,首先要确保在viewcontroller中有可用的图像(通过在storyboard上和viewcontroller中创建图像之间的关联).

让我们假装它叫做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)

这实际上会将图像动态分配给图像.你可以这样做,或者如果你真的做了一些简单的事情,就像点击按钮一样,你可以采取我上面指定的更简单的方法.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读