swift - 这个 Swift 函数创建的对象到哪里去了?

标签 swift github

我正在查看 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。请注意,在错误情况下使用相同的回调 - 然后 datanilerror 则不是。

关于swift - 这个 Swift 函数创建的对象到哪里去了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074870/

相关文章:

swift - 如何从一个 View Controller 向另一个 View Controller 发送和接收 UISlider 值

swift - 将 U+20000 或以上的 UnicodeScalar 附加到 String

swift - 如何更正自定义单元格中计时器倒计时中数字的跳过?

android - Git rev-list 在 Android Studio build.gradle 中不起作用

git - 如何修复 Github Travis CI 构建中 git 子模块更新的权限被拒绝(公钥)错误?

java - 在 github 存储库中托管私有(private) Maven Artifact

swift - 使用多个搜索栏和交互式弹出手势时导航项 View 消失

swift - 在主单元格中显示动态附加单元格

git - 了解 Git 和 GitHub 的基础知识

javascript - 如何将另一个 html 文件添加到 github.io 页面中的现有 html 中?