Skip to content

Instantly share code, notes, and snippets.

@taberh
Created July 2, 2012 08:34
Show Gist options
  • Save taberh/3031954 to your computer and use it in GitHub Desktop.
Save taberh/3031954 to your computer and use it in GitHub Desktop.
get bitmap from image
unsigned char *getBitmapFromImage(UIImage *image)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL) {
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
CGSize size = image.size;
void *bitmapData = malloc(size.width * size.height * 4);
if (bitmapData == NULL) {
fprintf(stderr, "Error: Memory not allocated");
return NULL;
}
CGContextRef = context = CGBitmapContextCreate(bitmapData, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
if (context == NULL) {
fprintf(stderr, "Error: Context not created");
free(bitmapData);
return NULL;
}
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGContextDrawImage(context, rect, image.CGImage);
unsigned char *data = CGBitmapContextGetData(context);
CGContextRelease(context);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment