Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Created September 3, 2012 21:27
Show Gist options
  • Save SlaunchaMan/3613715 to your computer and use it in GitHub Desktop.
Save SlaunchaMan/3613715 to your computer and use it in GitHub Desktop.
Getting Image Data in iOS
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(colorSpace) + 1; // Add 1 for the alpha channel
size_t bitsPerComponent = 8;
size_t bytesPerPixel = (bitsPerComponent * numberOfComponents) / 8;
size_t bytesPerRow = bytesPerPixel * width;
uint8_t *rawData = (uint8_t*)calloc(width * height * numberOfComponents, sizeof(uint8_t));
CGContextRef context = CGBitmapContextCreate(rawData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [anImage CGImage]);
CGContextRelease(context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment