]> git.r.bdr.sh - rbdr/captura/blame_incremental - Captura/Data/GifRenderer.swift
Complete initial release
[rbdr/captura] / Captura / Data / GifRenderer.swift
... / ...
CommitLineData
1import UniformTypeIdentifiers
2import SwiftUI
3import CoreGraphics
4
5struct 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}