我在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中?
谢谢
答案 0 :(得分:1)
结果GIF以NSData
传递到callback
,然后您可以从中创建NSImage
。请注意,在错误条件中使用相同的回调 - 然后data
为nil
而error
则不是。