Swift - Create a GIF from Images and turn it into NSData

Jac*_*nig 5 gif nsdata ios swift

This might be an amateur question, but although I have searched Stack Overflow extensibly, I haven't been able to get an answer for my specific problem.

I was successful in creating a GIF file from an array of images by following a Github example:

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)
}
Run Code Online (Sandbox Code Playgroud)

Now, I would like to turn the actual GIF into NSData so I can upload it to Firebase, and be able to retrieve it on another device.

To achieve my goal, I have two options: Either to find how to use the code above to extract the GIF created (which seems to directly be created when creating the file), or to use the images on the function's parameters to create a new GIF but keep it on NSData format.

Does anybody have any ideas on how to do this?

smn*_*mnk 0

由于六个月以来没有人继续前进,我将把 @Sachin Vas 评论的答案放在这里:

您可以使用获取数据NSData(contentsOf: URL)