我目前正在尝试找到一种从 iOS 中的 UIImageview 中找到一些 rgba 值的方法。例如,我想0-0-0
在白色背景上找到所有黑色像素1-1-1
并输出它们的坐标。
我当前的代码(改编自另一个stackoverflow帖子):
UIImage *myPicture = [UIImage imageNamed:@"somePicture.png"];
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(myPicture.CGImage));
myWidth = CGImageGetWidth(myPicture.CGImage);
myHeight = CGImageGetHeight(myPicture.CGImage);
const UInt8 *pixels = CFDataGetBytePtr(pixelData);
UInt8 blackThreshold = 10;
int bytesPerPixel_ = 4;
for( x = 0; x < myWidth; x++)
for( y = 0; y < myHeight; y++)
{
{
int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_;
UInt8 redVal = pixels[pixelStartIndex + 1];
UInt8 greenVal = pixels[pixelStartIndex + 2];
UInt8 blueVal = pixels[pixelStartIndex + 3];
if(redVal < blackThreshold && blueVal < blackThreshold && greenVal < blackThreshold) {
NSLog(@"x coordinate = %@", x);
NSLog(@"y coordinate = %@", y);
}
}
}
}
有人可以查看代码并就可能的解决方案提供一些反馈吗?