2

I have a Folder of Images in Images.xcassets. I want to display all the images from only that particular folder and load those images in a collection view. I know i will have to write this code to display the images

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

I will have to run a for loop to display all the images in the collection view. But i do not know how to access all the images from the Images.xcassets folder

Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54
  • One way is to add the names of all images to an array and use it , otherwise create a script like this http://stackoverflow.com/a/27930578/4518753 – good4pc Aug 23 '16 at 08:36
  • @good4pc Is there a way to give the path of the folder and create an array of its contents, then access it using a loop? – Khadija Daruwala Aug 23 '16 at 08:40

1 Answers1

1

Well, according to my understandings, a folder inside the Assets.xcassets folder means nothing. If there is a folder called Jack, Apple then inside Jack you have Apple. Then you try running imageView.image = UIImage(named: "Apple"), yes this will work. You don't need imageView.image = UIImage(named: "Jack/Apple").

So if you just name your images something like image1, image2, image3, image4, image5 then you can do something like.

for index in 0 ..< 5 {
    imageView.image = UIImage(named: String(format: "image%i", index))
}

It doesn't matter where the images are located in the files.

impression7vx
  • 1,728
  • 1
  • 20
  • 50