Created
August 12, 2011 12:41
-
-
Save akisute/1141953 to your computer and use it in GitHub Desktop.
Create an animated gif file from images in iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
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?
made an adaptation for swift 2 and NSImage: https://gist.github.com/ScottBuchanan/c63129cc2c5bb824fbac21c2d389ed22
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
(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);
}