3

So far I have created a folder of images and I am able to assign 1 of the images to a UIImageView.

But what I want to do now is create a random generator, that will pick a random image from the folder, so if I was to create a button, I could display all of the images separately and randomly on a single UIImageView.

However, this folder is going to have more than 100 images, so I don't want to hard code each one into an array. Also, the names of the image has to be unique to the image itself.

I have been searching online but cannot find information on how to code what will suit my needs. So how would I get and display a random image(s) from a folder in swift?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Just get your folder contents (array of urls) and randomly pick one – Leo Dabus Aug 06 '16 at 21:39
  • 1
    http://stackoverflow.com/questions/27259332/get-random-elements-from-array-in-swift/27261991?s=3|0.0664#27261991 http://stackoverflow.com/questions/27721418/getting-list-of-files-in-documents-folder/27722526?s=1|1.1551#27722526 – Leo Dabus Aug 06 '16 at 21:44

2 Answers2

5

You can name your images like this

image_0
image_1
image_2

Then use this code to populate your UIImageView

let numberOfImages: UInt32 = 100
let random = arc4random_uniform(numberOfImages)
let imageName = "image_\(random)"
imageView.image = UIImage(named: imageName)

Update

Since you don't want to rename your images you can create a plist file (type Array) where you put all the names of your images

enter image description here

And finally

func loadRandomImage() {
    let path = NSBundle.mainBundle().pathForResource("Images", ofType: "plist")!
    let names = NSArray(contentsOfFile: path) as! [String]
    let random = Int(arc4random_uniform(UInt32(names.count)))
    let imageName = names[random]
    imageView.image = UIImage(named: imageName)
}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • 1
    The names don't matter, just get your folder contents, use its count to limit the random index generated. http://stackoverflow.com/questions/27259332/get-random-elements-from-array-in-swift/27261991?s=3|0.0664#27261991 – Leo Dabus Aug 06 '16 at 21:42
  • @AndrewMontague: Are you using `Assets.xcassets`? That's where you should put the images for a bunch of reasons – Luca Angeletti Aug 06 '16 at 21:49
  • @AndrewMontague: Using Assets.xcassets is important, especially if you are making a game. – Luca Angeletti Aug 06 '16 at 21:57
  • @AndrewMontague: I added a new section to my answer, please check it out. Or try the solution suggested by _@Leo Dabus_. – Luca Angeletti Aug 06 '16 at 22:07
  • Thank you, this method works exactly for what I wanted, I really appreciate your help! –  Aug 06 '16 at 22:49
2

You either need to create an array of string filenames and pick a random filename from the array, or you need to use the file manager (NSFileManager) to get a list of the files in your folder and then pick from THAT array. Nether is very difficult.

Duncan C
  • 128,072
  • 22
  • 173
  • 272