Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create an animated gif file from images in iOS
#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
- (void)exportAnimatedGif
{
UIImage *shacho = [UIImage imageNamed:@"shacho.png"];
UIImage *bucho = [UIImage imageNamed:@"bucho.jpeg"];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
kUTTypeGIF,
2,
NULL);
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:2] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
CGImageDestinationAddImage(destination, shacho.CGImage, (CFDictionaryRef)frameProperties);
CGImageDestinationAddImage(destination, bucho.CGImage, (CFDictionaryRef)frameProperties);
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
NSLog(@"animated GIF file created at %@", path);
}
@daniloster

This comment has been minimized.

Copy link

commented May 21, 2012

Why when I use CGImageDestinationCreateWithURL my iOS simulator not run? When i comment from CGImageDestinationCreateWithURL to the end the Xcode calls for simulator.

@akisute

This comment has been minimized.

Copy link
Owner Author

commented May 22, 2012

hmm, strange. Does it really work in your iOS device? Also, those APIs are only available in iOS 4 or above so be wary of it.

@daniloster

This comment has been minimized.

Copy link

commented May 22, 2012

No, it doesn't. I've not tested on device yet. I am using this code to create a titanium module. When I've compiled this code through the xcode 4.3 it showed "Build Succedded", but when I commented the code from "CGImageDestinationRef destination = CGImageDestinationCreateWithURL..." not only showed that message, but also ran the simulator. I've noticed that my module worked when I've commented that code too.
So, what am I supposed to do to solve it. Needs It a specific library linked to project?

@akisute

This comment has been minimized.

Copy link
Owner Author

commented May 23, 2012

Oh I see. I'm not familiar with the Titanium and its plugins so I can't say this code is valid for a Titanium module. I think it's fine though...

I'm a little bit confused with your situation, so let me clear things:

  1. When you build my original code above, you got a build error, or runtime error, so that you can't run on the iOS simulator.
  2. When you build the code with the comments like below, you got no errors, so that you can run on the iOS simulator.
- (void)exportAnimatedGif
{
    UIImage *shacho = [UIImage imageNamed:@"shacho.png"];
    UIImage *bucho = [UIImage imageNamed:@"bucho.jpeg"];

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];

// Commented out starts here
/*
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
                                                                        kUTTypeGIF,
                                                                        2,
                                                                        NULL);

    NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:2] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
                                                                                                   forKey:(NSString *)kCGImagePropertyGIFDictionary];
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                              forKey:(NSString *)kCGImagePropertyGIFDictionary];

    CGImageDestinationAddImage(destination, shacho.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, bucho.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
    NSLog(@"animated GIF file created at %@", path);
*/
// Commented out ends here
}

Are they true or not? Also, give me any kind of error messages you got.

Needs It a specific library linked to project?

As you can see you have to link ImageIO.framework and MobileCoreServices.framework (both of them are provided by iOS runtime) when build, but nothing else.

@daniloster

This comment has been minimized.

Copy link

commented May 23, 2012

The second case is right, but the first not. In first case I've not got an error. The build is succeded, though not starts the ios simulator. there is no message error to both.

@akisute

This comment has been minimized.

Copy link
Owner Author

commented May 26, 2012

Hmm that's so weird. I can understand the case that the simulator tries to launch your app then immediately crashes, but if the simulator doesn't even start at all as you speak, I have completely no clue... I'm sorry :(

@rashidasgari

This comment has been minimized.

Copy link

commented Dec 2, 2013

You are a life saver! thank you! I was wondering if it is possible to increase the speed of animation.

@SeanDunford

This comment has been minimized.

Copy link

commented Apr 11, 2014

@akisute I know this is a pretty old post but when I save images like this they playback super duper slow. I have tried to use
[NSNumber numberWithFloat:0.0] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
and
[NSNumber numberWithInt:0.0] forKey:(NSString *)kCGImagePropertyGIFDelayTime]

but the frames still play very very slow. Any thoughts?

@SeanDunford

This comment has been minimized.

Copy link

commented Apr 11, 2014

  • (void)exportAnimatedGif
    {
    //Completely in memory approach. Have to be careful with this as it *WILL crash the app and maybe phone if too much
    //is loaded into memory. source: https://gist.github.com/akisute/1141953

    int count = [images count] -1;
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated5.gif"];
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path],
    kUTTypeGIF,
    count,
    NULL);

    NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:
    [NSDictionary dictionaryWithObject: [NSNumber numberWithFloat:0.0] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
    forKey:(NSString *)kCGImagePropertyGIFDictionary];
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:
    [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
    forKey:(NSString *)kCGImagePropertyGIFDictionary];

    for(int i = 1; i < [images count]; i ++){
    int j = [images count] - i;
    UIImage *img = [images objectAtIndex:j];
    CGImageDestinationAddImage(destination, img.CGImage, (__bridge CFDictionaryRef)frameProperties);
    }

    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
    NSLog(@"animated GIF file created at %@", path);
    }

@theSiberman

This comment has been minimized.

Copy link

commented Mar 25, 2015

Any luck with getting an acceptable frame rate? Mine is incredibly slow, and looping is dodgy as well? Is there a way to set dither settings etc?

@ScottBuchanan

This comment has been minimized.

Copy link

commented Dec 17, 2016

@sunwell

This comment has been minimized.

Copy link

commented May 25, 2017

I have a problem with this piece of code, when I use this snippet to generate one gif with two static image, it works fine. But when I use ffmpeg to transform this gif to mp4, this mp4 file has a problem that one image is at beginning position and another is at the end. So when I loop this video, it acts strange.
this is my gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.