2016-08-22 47 views
1

我在看这个分支在GitHub上:这个Swift函数创建的对象在哪里?

https://gist.github.com/westerlund/eae8ec71cdac88be7c3a

这是函数,用于斯威夫特创建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中?

谢谢

+1

进入回调?这行'回调(数据:NSData(contentsOfURL:url),错误:无)' –

+0

那么我应该使用NSData创建一个UIImage呢? –

+1

关于正确的声音,介绍数据为零且错误不是的情况。 –

回答

1

结果GIF作为NSData传递到callback那么你可以创建你从它NSImage。请注意,在错误情况下使用相同的回叫 - 那么datanilerror不是。

+0

对不起,是一个痛苦,但我不知道如何从NSData创建UIImage。所有与UIImage和数据相关的函数都使用类型“数据”,这显然不等同于NSData,因为当我尝试使用由回调创建的数据时出现类型错误... –

+1

有一个需要NSData切换到页面上的Swift):https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/occ/instm/UIImage/initWithData: –