timshadel
8/1/2016 - 9:21 PM

Programmatically create APNG files

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)
    }