Reputation: 197
I want my users to be able to view every image that i have in images.xcassets in a collection view, but theres about 50 image, and I'd rather not manually type the name for every one. Is there a way that I can load them all without doing this?
Upvotes: 1
Views: 402
Reputation: 1222
its not possible according the this post
Unfortunately it is not possible to load images from any car file aside from the one that Xcode compiles into your main bundle, as +imageNamed: does not accept a bundle parameter, which is what is needed to do so (and even then it would only be able to open a single asset catalog in a single bundle).
but there is always some solutions(today we have 2).
rename all images in your images.xcassets
(lets say you have 20 message) to 0..19 and after that take image using loop like so:
var imageList = [UIImage]()
for i in 0 ..< 20{
imageList.append(UIImage(named: String(i))!)
}
//imageList will hold all your images
or you can put your images inside your project folder and search for file that contain the known images types like so:
let fm = NSFileManager.defaultManager()
let path = NSBundle.mainBundle().resourcePath!
let items = try! fm.contentsOfDirectoryAtPath(path)
var imageList = [UIImage]()
for item in items {
if item.hasSuffix("png") || item.hasSuffix("jpg") || item.hasSuffix("jpeg") {
imageList.append(UIImage(named: item)!)
}
}
let me know if you need more explanation
Upvotes: 2