Skip to content

Instantly share code, notes, and snippets.

@kosso
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosso/bc081f99da6585e85476 to your computer and use it in GitHub Desktop.
Save kosso/bc081f99da6585e85476 to your computer and use it in GitHub Desktop.
useful gif thing
-(void)createGIF:(id)args
{
//NSURL *filename = [NSURL URLWithString:[args objectAtIndex:0]];
NSString *filename = [args objectAtIndex:0];
NSArray *images = [args objectAtIndex:1];
float delay = [TiUtils floatValue:[args objectAtIndex:2]];
float loop = [TiUtils floatValue:[args objectAtIndex:3]];
// CFStringRef CFloop = (__bridge CFStringRef)loop;
// CFStringRef CFdelay = (__bridge CFStringRef)delay;
BOOL newImagesIsArray = [images isKindOfClass:[NSArray class]];
if(newImagesIsArray)
{
NSUInteger kFrameCount = [images count];
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:delay] forKey:(NSString *)kCGImagePropertyGIFDelayTime] forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSDictionary *fileProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:loop] forKey:(NSString *)kCGImagePropertyGIFLoopCount] forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:filename];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
// Read the array of image urls. (provide full path)
for (NSString * thisImage in images)
{
NSURL *theurl = [NSURL URLWithString:thisImage];
UIImage *image = [UIImage imageWithContentsOfFile:[theurl path]];
if (image != nil) {
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
image = nil;
}
}
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"[INFO] failed to finalize image destination");
}
CFRelease(destination);
NSLog(@"[INFO] GIF created at : %@", fileURL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment