Load GIF from documents directory and convert it to data

246 views Asked by At
 guard let imageData = try? Data(contentsOf: bundleURL) else {
            print("Cannot turn image named \"\(gifNamed)\" into NSData")
            return nil

        }

I want to use GIF path in documents directory and extract the data from it not with bundleUrl but with GIF path in : file:///var/mobile/Containers/Data/Application/5615E8E1-F120-4171-A7CE-B2A9F2E8FC19/Documents/test.gif

1

There are 1 answers

8
Bulat Yakupov On

If I understand correctly you want to get data from gif that is located in you app's Documents directory. Your code may look like that:

func getDataOf(gifNamed: String) -> Data? {
    // Get url of Documents directory
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    guard let documentsDirectory = paths.first else { return nil }

    // Get url of your gif
    guard let gifURL = documentsDirectory.appendingPathComponent(gifNamed) else { return nil }

    // Extract the data of your gif
    var data: Data?
    do {
        data = try Data(contentsOf: bundleURL)
    } catch {
        print(error)
    }

    return data
}