Skip to content

Instantly share code, notes, and snippets.

@tgaul
Created January 24, 2011 04:27
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 tgaul/792829 to your computer and use it in GitHub Desktop.
Save tgaul/792829 to your computer and use it in GitHub Desktop.
Generating a 1-bit GIF with ImageIO
- (void) generate1BitGIF
{
UIImage* image = [ UIImage imageNamed: @"TestImage.png" ];
NSFileManager* fm = [ NSFileManager defaultManager ];
NSURL* destDir = [ fm URLForDirectory: NSDocumentDirectory
inDomain: NSUserDomainMask
appropriateForURL: nil
create: YES
error: nil ];
NSURL* destUrl = [ destDir URLByAppendingPathComponent: @"test.gif" ];
CGImageDestinationRef dst = CGImageDestinationCreateWithURL( (CFURLRef) destUrl, kUTTypeGIF, 1, NULL );
const uint8_t colorTable[ 6 ] = { 0, 0, 0, 255, 255, 255 };
NSData* colorTableData = [ NSData dataWithBytes: colorTable length: 6 ];
NSDictionary* gifProps = [ NSDictionary dictionaryWithObject: colorTableData
forKey: (NSString*) kCGImagePropertyGIFImageColorMap ];
NSDictionary* imgProps = [ NSDictionary dictionaryWithObject: gifProps
forKey: (NSString*) kCGImagePropertyGIFDictionary ];
CGImageDestinationAddImage( dst, image.CGImage, (CFDictionaryRef) imgProps );
CGImageDestinationFinalize( dst );
CFRelease( dst );
}
@coryl
Copy link

coryl commented Jul 10, 2013

What does this do? Tried using it, colorTable doesn't seem to do anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment