5

Is there a good analog to NSBitmapImageRep in UIKit? Specifically I'm looking for similar methods to setPixel:atX:y: and getPixel:atX:y:. I have found this technote, and it does get me most of the way there using CGImage, but how do I know what order the pixels are in?

1 Answer 1

4
CGImageGetBitsPerComponent( image );
CGImageGetBitsPerPixel( image );
CGImageGetBytesPerRow( image );
CGImageGetBitmapInfo( image );
CGColorSpaceGetModel( CGImageGetColorSpace( image ) );
CGColorSpaceGetNumberOfComponents( CGImageGetColorSpace( image ) );

Using the above calls, you can figure out how each pixel is laid out. Each pixel for x,y will start at:

length = CGImageGetBitsPerPixel(image)/CHAR_BIT;
offset = y*CGImageGetBytesPerRow(image) + x*length;

A typical 32 bit ARGB PNG might be:

8 = CGImageGetBitsPerComponent( image );
32 = CGImageGetBitsPerPixel( image );
kCGImageAlphaFirst = CGImageGetBitmapInfo( image );
4 = CGColorSpaceGetNumberOfComponents( CGImageGetColorSpace( image ) );
kCGColorSpaceModelRGB = CGColorSpaceGetNumberOfComponents( CGImageGetColorSpace( image ) );
2
  • Is there a possibility of a non-RGB layout? That was the main thing I was worried about, assuming that every image I find will be RGB.
    – paxswill
    Jun 29, 2010 at 15:53
  • Yes, use CGColorSpaceGetModel if you do not know the color space. Jul 3, 2010 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.