Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created September 24, 2010 23:24
Show Gist options
  • Save DylanLukes/596227 to your computer and use it in GitHub Desktop.
Save DylanLukes/596227 to your computer and use it in GitHub Desktop.
GLvoid* load_image(NSString* imagename){
CFURLRef url = (CFURLRef)[[NSBundle mainBundle] URLForImageResource:imagename];
CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
CGImageRef img = CGImageSourceCreateImageAtIndex(source, 0, NULL);
if(!img) NSLog(@"Image loading failed for \"%@\"", imagename);
NSInteger width = CGImageGetWidth(img);
NSInteger height = CGImageGetHeight(img);
unsigned char *data = (unsigned char*) malloc(width*height*4);
// Create a context to draw the new bitmap into
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), img);
CGContextRelease(context);
CGImageRelease(img);
CFRelease(source);
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment