0

I have a method to get a specific pixel on a UIImage, but it doesn't seem to be working so well. The colors don't seem right. Can anyone determine why?

+ (UIColor *)colorAtPixelX:(int)x Y:(int)y image:(UIImage *)img{

    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(img.CGImage));
    const UInt8* data = CFDataGetBytePtr(pixelData);

    int pixelInfo = ((img.size.width  * y) + x ) * 4; 

    UInt8 red = data[pixelInfo];
    UInt8 green = data[(pixelInfo + 1)];
    UInt8 blue = data[pixelInfo + 2];
    UInt8 alpha = data[pixelInfo + 3];
    CFRelease(pixelData);

    UIColor *col = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];

    return col;

}
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

1 Answers1

0

Check out this post. You can then turn the RGBAPixel instance into a UIColor using [UIColor colorWithRed:green:blue:alpha:];

Community
  • 1
  • 1
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58