这可能是一个业余问题,但是虽然我已经可扩展地搜索了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格式.
有没有人对如何做到这一点有任何想法?