1

I would like to create a gif from [CGImage] directly as Data. Using the function

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)

}

...from post (Swift - Create a GIF from Images and turn it into NSData).

I would like to save this gif as transformable data as an entity.property.

Thus I would like to skip the step of saving it to documents.url and basically create a gif as data from [CGImage] and set as entity.gif.

Is there a CGImageCreate function that can take [CGImage] and output a gif as data?

Community
  • 1
  • 1
Chameleon
  • 1,608
  • 3
  • 21
  • 39

0 Answers0