Swift 显示图片 UIImageview [英] Swift display image UIImageview

查看:31
本文介绍了Swift 显示图片 UIImageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I display image inside UIImageview? Then change images according to pressed button.

我很新,请放轻松.

推荐答案

要做到这一点,首先要确保在视图控制器中有可用的图像(通过在故事板上的图像和视图控制器之间创建关联).

To do this, first make sure you have the image available in the viewcontroller (by creating an association between the image on your storyboard, and in your viewcontroller).

假设它叫做 imageView.

Let's pretend it's called imageView.

以编程方式,您可以说:

Programmatically, you can say:

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

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

What I actually ended up doing is creating a static function responsible for determining which image to return as such:

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))

}

既然已经创建了上面的函数,你可以做这样的事情:

So now that the function above has been created, you could just do something like this:

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

这实际上会将图像动态分配给图像.您可以直接执行此操作,或者如果您实际上是在做一些简单的事情,例如将其放入按钮单击中,您可以采用我上面指定的更简单的方法.

This would actually assign the images dynamically to the image. You could just do this, or if you're literally doing something as simple as putting it into a button click you can just take the easier approach I specified above.

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

Thanks let me know if any questions!

这篇关于Swift 显示图片 UIImageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆