这个Swift函数创建的对象去哪里了?
我正在查看GitHub上的这个分支: 此函数用于从Swift中的UIImages创建GIF:这个Swift函数创建的对象去哪里了?,swift,github,Swift,Github,我正在查看GitHub上的这个分支: 此函数用于从Swift中的UIImages创建GIF: func createGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double, callback: (data: NSData?, error: NSError?) -> ()) { let fileProperties = [kCGImagePropertyGIFDictionary as Str
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())
}
}
func createGIF(带图像:[UIImage],循环计数:Int=0,帧延迟:Double,回调:(数据:NSData?,错误:NSError?)->()
让fileProperties=[kCGImagePropertyGIFDictionary作为字符串:[kCGImagePropertyGIFLoopCount作为字符串:loopCount]]
让frameProperties=[kCGImagePropertyGIFDictionary作为字符串:[kCGImagePropertyGIFDelayTime作为字符串:frameDelay]]
让documentsDirectory=NSTemporaryDirectory()
让url=NSURL(fileURLWithPath:documentsDirectory)?.URLByAppendingPathComponent(“animated.gif”)
如果让url=url{
让destination=CGImageDestinationCreateWithURL(url,kuttypegf,UInt(images.count),nil)
CGImageDestinationSetProperties(目标,文件属性)
对于0中的i.结果GIF作为NSData
传递到回调中,然后您可以从中创建NSImage
。请注意,在错误条件下使用相同的回调-然后数据
为nil
而错误
不是。传递到回调中?这一行回调(数据:NSData:NSData(contentsofull:url),error:nil)
那么我应该使用NSData来创建UIImage?听起来不错,注意数据为零而error不是的情况。谢谢,我想我现在明白了。很抱歉,我不知道如何从NSData创建UIImage。所有与UIImage和数据相关的函数都使用“data”类型,这显然不等同于NSData,因为我尝试使用回调创建的数据时出现类型错误…有一个接受NSData的ctor(在页面上切换到Swift)::