Skip to content

Instantly share code, notes, and snippets.

@hellopatrick
Created January 31, 2012 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellopatrick/1711925 to your computer and use it in GitHub Desktop.
Save hellopatrick/1711925 to your computer and use it in GitHub Desktop.
Color Table
//
// HRAppDelegate.m
// ColorTableTest
//
// Created by Patrick Rogers on 1/17/12.
// Copyright (c) 2012 Hello Resolven. All rights reserved.
//
#import "HRAppDelegate.h"
// 1 will produce correctly colored GIF, 2 will produce wrongly colored GIF.
#define NUMBER_OF_FRAMES 2
@implementation HRAppDelegate
@synthesize window = _window;
// create image from path
- (CGImageRef)imageWithPath:(NSString *)path {
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)[NSData dataWithContentsOfFile:path]);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
return image;
}
// properties to be applied to each frame... such as setting delay time & color map.
- (NSDictionary *)frameProperties {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
[dict setObject:[NSNumber numberWithFloat:0.5] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
const uint8_t colorTable[ 9 ] = { 0, 0, 0, 128, 128, 128, 255,255,255};
NSData* colorTableData = [ NSData dataWithBytes: colorTable length: 9 ];
[dict setObject:colorTableData forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
NSDictionary *frameProps = [ NSDictionary dictionaryWithObject: dict
forKey: (NSString*) kCGImagePropertyGIFDictionary ];
return frameProps;
}
// properties to apply to entire GIF... such as loop count (0 = infinite) and no global color map.
- (NSDictionary *)gifProperties {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
[dict setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
[dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary *gifProps = [ NSDictionary dictionaryWithObject: dict
forKey: (NSString*) kCGImagePropertyGIFDictionary ];
return gifProps;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// load images
NSString *pathOne = [[NSBundle mainBundle] pathForImageResource:@"one"];
NSString *pathTwo = [[NSBundle mainBundle] pathForImageResource:@"two"];
CGImageRef imageOne = [self imageWithPath:pathOne];
CGImageRef imageTwo = [self imageWithPath:pathTwo];
// output final gif to desktop.
NSURL *destUrl = [NSURL fileURLWithPath:[@"~/Desktop/test.gif" stringByExpandingTildeInPath]];
CGImageDestinationRef dst = CGImageDestinationCreateWithURL( (__bridge CFURLRef) destUrl, kUTTypeGIF, NUMBER_OF_FRAMES, NULL );
// add first image.
CGImageDestinationAddImage( dst, imageOne, (__bridge CFDictionaryRef) [self frameProperties] );
// add second image if NUMBER_OF_FRAMES is 2... set to 1 see the kCGImagePropertyGIFImageColorMap property actually work.
#if (NUMBER_OF_FRAMES == 2)
NSLog(@"adding second one.");
CGImageDestinationAddImage( dst, imageTwo, (__bridge CFDictionaryRef) [self frameProperties] );
#endif
CGImageDestinationSetProperties(dst, (__bridge CFDictionaryRef) [self gifProperties]);
CGImageDestinationFinalize( dst );
// cleanup
CFRelease(imageTwo);
CFRelease(imageOne);
CFRelease( dst );
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment