]>
Commit | Line | Data |
---|---|---|
f5d16c1c RBR |
1 | import UniformTypeIdentifiers |
2 | import SwiftUI | |
3 | import CoreGraphics | |
4 | ||
5 | struct GifRenderer { | |
6 | static func render(_ images: [CGImage], at fps: Int, to url: URL) async { | |
7 | let framedelay = String(format: "%.3f", 1.0 / Double(fps)) | |
8 | let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] | |
9 | let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFUnclampedDelayTime as String: framedelay]] | |
10 | let cfURL = url as CFURL | |
11 | if let destination = CGImageDestinationCreateWithURL(cfURL, UTType.gif.identifier as CFString, images.count, nil) { | |
12 | CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?) | |
13 | for image in images { | |
14 | CGImageDestinationAddImage(destination, image, gifProperties as CFDictionary?) | |
15 | } | |
16 | CGImageDestinationFinalize(destination) | |
17 | } | |
18 | } | |
19 | } |