如何在UIImageview中显示图像?然后根据按下的按钮更改图像。
我很新,请放松。
要做到这一点,首先确保在视图控制器中有可用的图像(通过在故事板上的图像和视图控制器中的图像之间创建关联)。
让我们假设它叫做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)
这实际上会将图像动态地分配给图像。你可以这样做,或者如果你真的在做一些简单的事情,比如把它放进一个按钮点击中,你可以采取我上面指定的更简单的方法。
谢谢,如果有任何问题请告诉我!