Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

How to access all images within specific directory?

This is what i need to access:

enter image description here

I have a stickers for every language and I need to get an array of urls of these images. How can I do that for ezample for en directory?

Upvotes: 1

Views: 55

Answers (2)

Tomasz Pe
Tomasz Pe

Reputation: 716

You could try to use DirectoryEnumerator

private func getStickersURL(for languageCode: String) -> [URL] {
    guard let resourcePath = Bundle.main.resourcePath else {
        return []
    }
    let dirPath = [resourcePath, "Stickers", languageCode].joined(separator: "\\")

    return FileManager.default
        .enumerator(atPath: dirPath)?
        .compactMap { $0 as? String }
        .compactMap {
            URL(
                fileURLWithPath: $0,
                relativeTo: URL(fileURLWithPath: dirPath)
            ).absoluteURL
        } ?? []
}

Upvotes: 1

Adis
Adis

Reputation: 4552

If I remember correctly, asset folder structure isn't preserved during compilation, so it doesn't matter how you organize them, they are stored flat in the app directory.

I suggest building a dictionary for your assets, and then fetching images that way, rather than using file structure.

Upvotes: 1

Related Questions