import UIKit import CoreServices extension UIImage { static func createGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double) -> Data? { let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]] as CFDictionary let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frameDelay]] as CFDictionary let cgImages = images.compactMap { $0.cgImage } let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("animated.gif") if let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, cgImages.count, nil) { CGImageDestinationSetProperties(destination, fileProperties) for i in 0..