我使用创建图像
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// more code - not relevant - removed for debugging
image = UIGraphicsGetImageFromCurrentImageContext(); // the image is now ARGB
UIGraphicsEndImageContext();
然后我尝试找到一个像素的颜色(使用 Minas Petterson 的代码:Get Pixel color of UIImage)。但由于图像现在是 ARGB 格式,我不得不修改代码:
alpha = data[pixelInfo];
red = data[(pixelInfo + 1)];
green = data[pixelInfo + 2];
blue = data[pixelInfo + 3];
然而这并没有奏效。
问题是(例如)一个红色像素,在 RGBA 中将表示为1001(实际上是 255 0 0 255,但为简单起见,我使用 0 到 1 值),在图像中表示为0011而不是(因为我想) 1100。任何想法为什么?难道我做错了什么?
PS。我必须使用的代码看起来必须是这样的:
alpha = 255-data[pixelInfo];
red = 255-data[(pixelInfo + 1)];
green = 255-data[pixelInfo + 2];
blue = 255-data[pixelInfo + 3];