我想从一组图像创建一个GIF,在图像过渡时,该图像应淡入/淡出(动画)
我创建了一个函数,可以从图像创建GIF,但是没有动画。
以下是我的代码:
var urls : URL? = nil
let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary
let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): 1.0]] as CFDictionary
let documentsDirectoryURL: URL? = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL: URL? = documentsDirectoryURL?.appendingPathComponent("animated.gif")
if let url = fileURL as CFURL? {
if let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, myImages.count, nil) {
CGImageDestinationSetProperties(destination, fileProperties)
for image in myImages {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, frameProperties)
}
}
if !CGImageDestinationFinalize(destination) {
print("Failed to finalize the image destination")
}
if let url = fileURL {
urls = url
} else {
print("No file url found!")
}
}
}
return (urls ?? nil)!