Pan MluvĨí Pan MluvĨí - 1 month ago 10
Swift Question

Swift: How to get image name from assets

I have 500mb of images in folders in assets. I don't know any of their names. Is there a way to access them...store them to Array and then display?

Example: I have 3 images stored in

Assets/folder1/folder2

folder 2 contains that 3 images..... I need to get those names.

enter image description here

Here is the reason I need this.... Ive been provided with library of car images and that images have illogical names.

enter image description here

Answer

You cannot do what you are describing while storing the images in the Assets Catalog. Fetching a resource from an Assets Catalog relies upon your knowing its actual name.

Instead, store these folders directly at the top level of your app bundle. Now you have an actual folder that you can get a reference to, and can ask the FileManager for the names of the images in the folder.

enter image description here

Example (Swift 2.2, sorry):

let f = NSBundle.mainBundle().URLForResource("carPix", withExtension: nil)!
let fm = NSFileManager()
let arr = try! fm.contentsOfDirectoryAtURL(f, includingPropertiesForKeys: nil, options: [])
print(arr) // the URLs of the two jpg files
Comments