Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 797
Rob Caraway Avatar asked Oct 10 '14 13:10

Rob Caraway


2 Answers

**

Solution 01 : Only saving the existing GIF file to Camera Roll

**

As I understand your problem. You are able to generate a GIF file but cannot save and also view it to the Camera Roll.

So I am attaching a sample test using existing GIF File.

Step 01. I copied a gif IMG_0009.GIF file in my Application Document directory.

Step 02 Than I use the below code to load this files NSData:

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"IMG_0009.gif"];

NSData *gifData = [NSData dataWithContentsOfFile:[fileURL path]];

Step 03: Now I save the file in the Media Directory:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:gifData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

    NSLog(@"Success at %@", [assetURL path] );
}];

The Asset URL is proper. Now you can check you media directory. you can locate the saved gif image.

Have Fun :)


**

Solution 02: Demo of Creating and saving GIF to Camera roll

**

I cloned some solution to show creating and saving of GIF files to Camera Roll. You can download and check my fork at github:

The demo creates a GIF file by taking 2 or more images and save in the Camera Roll Directory

https://github.com/bllakjakk/Giraffe

The main Code to focus is like below:

[export encodeToFile:tempFile callback:^(NSString * aFile) {

    NSLog(@"Path: %@", aFile);

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:aFile]];

    [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

        NSLog(@"Success at %@", [assetURL path] );
    }];

}];

It uses the library as I mentioned in my solution before http://jitsik.com/wordpress/?p=208

How to verify:

Step 01: Run the demo project.

Step 02: As directed by the application add 2 images and click Export.

Step 03: Now check the camera roll you will find the created gif.

Previous:

GIF is a proprietary format, so you would need a 3rd party lib to save it.

check following link: http://jitsik.com/wordpress/?p=208

like image 127
bllakjakk Avatar answered Sep 18 '22 00:09

bllakjakk


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.

like image 20
Rob Caraway Avatar answered Sep 20 '22 00:09

Rob Caraway