iosobjective-cxcodegifalassetslibrary

Save generated GIF to camera roll?


Thanks for reading. I've created a GIF using methods from this question:

Create and and export an animated gif via iOS?

I'm trying to use the only method that appears to be able to save non JPG/PNG images to the camera roll, ALAssetLibrary's writeImageDataToSavedPhotosAlbum:metadata:completionBlock:

I save the Gif to the temp Directory like this:

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:@"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];

Then access the NSData like:

NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];

The GIF is created as I'm able to display it in a UIImageView, but when I try to save it, the method returns as a success (no error) but doesn't actually save (returns Nil for the NSURL * assetURL and does not appear in the camera roll).

How can I get my GIFs to save successfully to the camera roll?


Solution

  • I found the issue was that I was unable to actually grab the GIF from the file. I switched from using CGImageDestinationCreateWithURL to CGImageDestinationCreateWithData and used a CFMutableDataRef to hold the Gif data. I don't know why, but that made saving to camera roll with writeImageDataToSavedPhotosAlbum work.