2016-08-22 50 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: –

相關問題