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