Search code examples
iosuiimageviewxcasset

iOS - How to set image to ImageView according to a specific value


I have several images, and I would like to set the image of my ImageView according to a specific value I get from web service.

For example if I got value "dog" I have to set image of a dog.

All the images are in my xcassets repertory.

Should I make a Switch for that ? Or is there a better way to do it ?


Solution

  • If the web service returns the same names used in your assets then you can just use the returned name directly as the name of the image to use like this:

    let newImage: UIImage? = UIImage(named: imageName) // imageName is the name returned to you by the web service.
    self.myUIImageView.image = newImage