Getting at CGImageRef pixel data

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

  1.  
  2. NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(cgimage));
  3. const char *bytes = (const char *)[data bytes];
  4. int len = [data length];
  5.  
  6. // Note: this assumes 32bit RGBA
  7. for (int i = 0; i < len; i += 4) {
  8. char r = bytes[i];
  9. char g = bytes[i+1];
  10. char b = bytes[i+2];
  11. char a = bytes[i+3];
  12. }
  13.  

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>