Getting at CGImageRef pixel data

Here is some code to read the actual image pixel data of a CGImageRef:

NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(cgimage));
const char *bytes = (const char *)[data bytes];
int len = [data length];
// Note: this assumes 32bit RGBA
for (int i = 0; i < len; i += 4) {
    char r = bytes[i];
    char g = bytes[i+1];
    char b = bytes[i+2];
    char a = bytes[i+3];

2 Replies to “Getting at CGImageRef pixel data”

Leave a Reply