Search code examples
iosswiftuiimagexcasset

Load image from xcassets without knowing the name


I have multiple images in my asset catalog as shown below. Is there a convenient method to load the images without using a hardcoded string every time:

var images = [UIImage]()
images.append(UIImage(named: "South_America-Argentina")!)
images.append(UIImage(named: "South_America-Bolivia")!)
images.append(UIImage(named: "South_America-Brazil")!)
// ...

xcassets


Solution

  • At the moment it is not possible to load images from xcassets without knowing the name. One way would be to store the image names in enums as @totiG already mentioned but with more than 100 flags this would be very time consuming.

    Fortunately there is a library who already did this for us: FlagKit