I have a images from the Images.xcassets folder and I want to load them into a UIImage programmatically using swift. The images are for a list so image 1 corresponds to list item 1. How can I do this?
Please give me some code examples!
2 Answers
let image = UIImage(named: "nameOfImageInXCAssets") I'm giving you the benefit of the doubt on this one, but this is a very straight forward read the manual case.
There's even a section called "Loading and Caching Images"
5 Comments
David Smith
Thank you for your help. I am getting errors when I try to load multiple images into UIImage. Is let and then else the correct strategy? Any Help?
Tim Bull
let is not mutable, so once set, you can't change it. If you want to change the image, you should use var instead. var image = UIImage(named: "xyz")
Tim Bull
Also to clarify, a UIImage is one image at a time, not multiple so I'm not quite clear on what you mean when you say "load multiple images into UIImage" - that's just something you can't do! You can however CHANGE the image - use var as I mentioned above.
David Smith
I am trying to assign a specific image to a specific list item. Each list item has a different image. I need to do this for each item on the list. This is for UITableView.
David Smith
I am having trouble getting the list item to link to the correct image.