]> git.r.bdr.sh - rbdr/captura/blob - Captura/Data/GifRenderer.swift
208d7db6f0a683c7bc4904fcc7680aea852ca76f
[rbdr/captura] / Captura / Data / GifRenderer.swift
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 }