Created
May 20, 2017 05:05
-
-
Save whit3hawks/8ba54962954cc2d0fea030d3a8f73463 to your computer and use it in GitHub Desktop.
Generating a GIF from UIImages (Swift 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateGif(photos: [UIImage], filename: String) -> 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 | |
} | |
func getDocumentsDirectory() -> URL? { | |
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment