我正在查看 GitHub 上的这个分支:
https://gist.github.com/westerlund/eae8ec71cdac88be7c3a
这是用于在 Swift 中从 UIImages 创建 GIF 的函数:
func createGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double, callback: (data: NSData?, error: NSError?) -> ()) {
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]]
let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frameDelay]]
let documentsDirectory = NSTemporaryDirectory()
let url = NSURL(fileURLWithPath: documentsDirectory)?.URLByAppendingPathComponent("animated.gif")
if let url = url {
let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, UInt(images.count), nil)
CGImageDestinationSetProperties(destination, fileProperties)
for i in 0..<images.count {
CGImageDestinationAddImage(destination, images[i].CGImage, frameProperties)
}
if CGImageDestinationFinalize(destination) {
callback(data: NSData(contentsOfURL: url), error: nil)
} else {
callback(data: nil, error: NSError())
}
} else {
callback(data: nil, error: NSError())
}
}
我不确定我是否理解发生了什么。该函数不应该返回一个要使用的对象吗?创建的 GIF 到哪里去了?是否可以在我的代码的其他部分使用它,例如将它放在 UIImageView 或 UIImage 中?
谢谢
最佳答案
结果 GIF 作为 NSData
传递到 callback
中,然后您可以从中创建您的 NSImage
。请注意,在错误情况下使用相同的回调 - 然后 data
为 nil
而 error
则不是。
关于swift - 这个 Swift 函数创建的对象到哪里去了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074870/