diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-28 08:53:26 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-28 08:53:26 +0200 |
| commit | f5d16c1c8c259966338b23afd407d142f72784aa (patch) | |
| tree | d8ef63436af5c954dac262b4766ece363ac10ac9 /Captura/Data/GifRenderer.swift | |
| parent | a4e804275517af683afa1733e2db7c383c306f2b (diff) | |
Separate some logic out of main app
Diffstat (limited to 'Captura/Data/GifRenderer.swift')
| -rw-r--r-- | Captura/Data/GifRenderer.swift | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Captura/Data/GifRenderer.swift b/Captura/Data/GifRenderer.swift new file mode 100644 index 0000000..208d7db --- /dev/null +++ b/Captura/Data/GifRenderer.swift @@ -0,0 +1,19 @@ +import UniformTypeIdentifiers +import SwiftUI +import CoreGraphics + +struct GifRenderer { + static func render(_ images: [CGImage], at fps: Int, to url: URL) async { + let framedelay = String(format: "%.3f", 1.0 / Double(fps)) + let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] + let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFUnclampedDelayTime as String: framedelay]] + let cfURL = url as CFURL + if let destination = CGImageDestinationCreateWithURL(cfURL, UTType.gif.identifier as CFString, images.count, nil) { + CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?) + for image in images { + CGImageDestinationAddImage(destination, image, gifProperties as CFDictionary?) + } + CGImageDestinationFinalize(destination) + } + } +} |