]> git.r.bdr.sh - rbdr/captura/blob - Captura/Data/GifRenderer.swift
bfbb289eea82e4eeb1a1af6d8d2e39d8b17a6587
[rbdr/captura] / Captura / Data / GifRenderer.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://captura.tranquil.systems.
16 */
17 import UniformTypeIdentifiers
18 import SwiftUI
19 import CoreGraphics
20
21 struct GifRenderer {
22 static func render(_ images: [CGImage], at fps: Int, to url: URL) async {
23 let framedelay = String(format: "%.3f", 1.0 / Double(fps))
24 let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
25 let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFUnclampedDelayTime as String: framedelay]]
26 let cfURL = url as CFURL
27 if let destination = CGImageDestinationCreateWithURL(cfURL, UTType.gif.identifier as CFString, images.count, nil) {
28 CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
29 for image in images {
30 CGImageDestinationAddImage(destination, image, gifProperties as CFDictionary?)
31 }
32 CGImageDestinationFinalize(destination)
33 }
34 }
35 }