Swift 3直接将gif创建为数据

时间:2017-03-09 18:36:22

标签: swift3 gif

我想直接从[CGImage]创建一个gif作为Data。使用功能

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)

}

...来自帖子(Swift - Create a GIF from Images and turn it into NSData)。

我想将此gif保存为可转换数据作为entity.property。

因此,我想跳过将其保存到documents.url的步骤,并基本上从[CGImage]创建一个gif作为数据并设置为entity.gif。

是否有CGImageCreate函数可以接受[CGImage]并输出gif作为数据?

0 个答案:

没有答案