4
這可能是一個業餘問題,但儘管我已經對Stack Overflow進行了可擴展搜索,但是我無法爲我的具體問題得到答案。Swift - 從圖像創建一個GIF並將其轉換爲NSData
我成功地通過以下Github的例子創建從圖像陣列GIF文件:
func createGIF(with images: [NSImage], name: NSURL, loopCount: Int = 0, frameDelay: Double) {
let destinationURL = name
let destinationGIF = CGImageDestinationCreateWithURL(destinationURL, kUTTypeGIF, images.count, nil)!
// This dictionary controls the delay between frames
// If you don't specify this, CGImage will apply a default delay
let properties = [
(kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): frameDelay]
]
for img in images {
// Convert an NSImage to CGImage, fitting within the specified rect
let cgImage = img.CGImageForProposedRect(nil, context: nil, hints: nil)!
// Add the frame to the GIF image
CGImageDestinationAddImage(destinationGIF, cgImage, properties)
}
// Write the GIF file to disk
CGImageDestinationFinalize(destinationGIF)
}
現在,我想談談實際GIF到NSData的,所以我可以把它上傳到火力地堡,並能夠在另一臺設備上檢索它。
爲了達到我的目標,我有兩個選擇:要麼找到如何使用上面的代碼來提取創建的GIF(這似乎是直接創建文件時創建的),或者在函數的參數上使用圖像創建一個新的GIF,但保持在NSData格式。
有沒有人有關於如何做到這一點的任何想法?
你正在寫的GIF到URL獲取數據。所以你可以使用'[NSData dataWithContentOfURL:destinationURL]'獲得數據。 –
謝謝.. !!這是一個有效的答案。這樣寫,所以我可以批准它。 –
如果你發現這個問題是相關的,我也將欣賞一個upvote :) –