Created
August 1, 2016 21:21
-
-
Save timshadel/1b8abfd6ee15b455a7e75d8591ce9c83 to your computer and use it in GitHub Desktop.
Programmatically create APNG files
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
// 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