Generate and export an animated gif via Swift 3.0?
<p>I've learned that the Image IO Framework has changed syntactically since iOS 9 according to the documentation, however I have done my research and the following code <em>seems</em> to be correct. I have to procedures listed below; one procedure takes images and writes those images to the application's document folder as a gif. I can confirm this works as I can view the actually gif file if I go to the app's documents folder using iTunes. Despite this, in the second procedure where I attempt to read from that same file, an error is throw which states the file at that path does not exist. I have posted the code below.</p>

<pre><code>class GifManager {
private func getDocumentsDirectory() -&gt; URL?  {
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
}

public func generateGif(photos: [UIImage], filename: String) -&gt; Bool {
    if let docsDirectory = getDocumentsDirectory() {
        let url = docsDirectory.appendingPathComponent(filename)
        let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
        let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.125]]
        if let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, photos.count, nil) {
            CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
            for photo in photos {
                CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
            }
            return CGImageDestinationFinalize(destination)
        }
    }
    return false
}

public func saveGifToCameraRoll(filename: String) {
    if let docsDirectory = getDocumentsDirectory() {
        let fileUrl: URL = docsDirectory.appendingPathComponent(filename)
        do {
            let data = try Data(contentsOf: fileUrl)
            if let _ = UIImage(data: data) {
                PHPhotoLibrary.shared().performChanges({
                    PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: fileUrl)
                    }, completionHandler: {completed, error in
                        if error != nil {
                            print("error")
                        } else if completed {
                            print("completed")
                        } else {
                            print("not completed")
                        }
                })
            }

        } catch let error {
            print(error)
        }
    }
}
</code></pre>