2

Have been looking into this topic, and use this method with varying luck:

-(RGBPixel*)bitmap{

    CGImageRef cgImage = [image CGImage];

    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);

    bitmapData = CGDataProviderCopyData(provider);  

   [self setPixelByteData: malloc( CFDataGetLength(bitmapData) )];

    CFDataGetBytes( bitmapData, CFRangeMake( 0, CFDataGetLength( bitmapData ) ), pixelByteData );

    pixelData   = (RGBPixel*) pixelByteData;

    colorLabel.text = [[NSString alloc]initWithFormat:@"Pixel data: red (%i), green (%i), blue (%i).", pixelData[100].red, pixelData[100].green, pixelData[100].blue] ;

    return pixelData;
}

The only thing that doesn`t work as I want is the red pixel data: it always says 255, while the green and blue behave as expected. What am I doing wrong?

Community
  • 1
  • 1

1 Answers1

2

As @Nick hinted, are you certain that you know the format of the image? bitsPerComponent, bitsPerPixel, bytesPerRow, ColorSpace ? The data could be in int or float format. It could be arranged RGB, RGBA, or ARGB.

Unless you created the image yourself from raw data you may not be certain. You can use functions like CGImageGetBitmapInfo(), CGImageGetAlphaInfo(), CGImageGetColorSpace() to find out.

hooleyhoop
  • 9,128
  • 5
  • 37
  • 58