Pan Mluvčí
Pan Mluvčí

Reputation: 1261

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

Upvotes: 2

Views: 9373

Answers (2)

Oksana
Oksana

Reputation: 119

Example above with Swift 3

if let f = Bundle.main.url(forResource: "carPix", withExtension: nil) {
    let fm = FileManager()
    return try? fm.contentsOfDirectory(at: f, includingPropertiesForKeys: nil, options: [])
}
return nil

Upvotes: 0

matt
matt

Reputation: 535989

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

Upvotes: 3

Related Questions