Skip to content

Instantly share code, notes, and snippets.

@timshadel
Created August 1, 2016 21:21
Show Gist options
  • Save timshadel/1b8abfd6ee15b455a7e75d8591ce9c83 to your computer and use it in GitHub Desktop.
Save timshadel/1b8abfd6ee15b455a7e75d8591ce9c83 to your computer and use it in GitHub Desktop.
Programmatically create APNG files
// By Heber Sheffield
public class func save(animatedImageSequence images: Array<CGImage>, withFrameDelay delay: CGFloat, numberOfLoops: Int, to url: URL) {
let fileProperties = [kCGImagePropertyPNGDictionary as String: [kCGImagePropertyAPNGLoopCount as String: numberOfLoops]]
let frameProperties = [kCGImagePropertyPNGDictionary as String: [kCGImagePropertyAPNGDelayTime as String: delay]]
guard let destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, images.count, nil) else { fatalError("couldn't create image destination for url: \(url)") }
CGImageDestinationSetProperties(destination, fileProperties)
for image in images {
CGImageDestinationAddImage(destination, image, frameProperties)
}
CGImageDestinationFinalize(destination)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment