Skip to content

Instantly share code, notes, and snippets.

@whit3hawks
Created May 20, 2017 05:05
Show Gist options
  • Save whit3hawks/8ba54962954cc2d0fea030d3a8f73463 to your computer and use it in GitHub Desktop.
Save whit3hawks/8ba54962954cc2d0fea030d3a8f73463 to your computer and use it in GitHub Desktop.
Generating a GIF from UIImages (Swift 3)
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