Created
July 2, 2012 08:34
-
-
Save taberh/3031954 to your computer and use it in GitHub Desktop.
get bitmap from image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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