【发布时间】:2017-04-17 14:39:52
【问题描述】:
这可能是一个业余问题,但尽管我已经广泛搜索了 Stack Overflow,但我无法获得针对我的具体问题的答案。
我按照 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,以便我可以将其上传到 Firebase,并能够在另一台设备上检索它。
为了实现我的目标,我有两个选择:要么找到如何使用上面的代码来提取创建的 GIF(这似乎是在创建文件时直接创建的),要么使用函数参数上的图像来创建一个新的 GIF,但保持 NSData 格式。
有人对如何做到这一点有任何想法吗?
【问题讨论】:
-
您正在将 GIF 写入 URL。所以你可以使用
[NSData dataWithContentOfURL: destinationURL]获取数据。 -
谢谢..!!这是一个有效的答案。就这样写,这样我就可以批准了。
-
如果您发现这个问题是相关的,我也将不胜感激:)
-
@JacoboKoenig 要制作多少个图像数组来制作 GIF .. 创建超过 60 加 320*320 像素的图像大小时出现问题...内存警告或崩溃报告。