Skip to content

Instantly share code, notes, and snippets.

@timshadel
Created August 1, 2016 21:21
Show Gist options
  • Select an option

  • Save timshadel/1b8abfd6ee15b455a7e75d8591ce9c83 to your computer and use it in GitHub Desktop.

Select an option

Save timshadel/1b8abfd6ee15b455a7e75d8591ce9c83 to your computer and use it in GitHub Desktop.

Revisions

  1. timshadel created this gist Aug 1, 2016.
    12 changes: 12 additions & 0 deletions APNG.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // 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)
    }