在UIImage上获取像素

时间:2012-01-21 22:49:32

标签: iphone ios image uiimage pixel

我有一种在UIImage上获取特定像素的方法,但它似乎没有那么好用。颜色似乎不对。任何人都可以确定原因吗?

+ (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;

}

1 个答案:

答案 0 :(得分:0)

结帐this post。然后,您可以使用[UIColor colorWithRed:green:blue:alpha:];

将RGBAPixel实例转换为UIColor