在UIImageView上查找特定像素的颜色



我正在处理油漆应用程序,并想构建撤消管理器。我存储了用户绘制的每个位置的像素坐标值,但我也想存储用户绘制的点的旧像素颜色,以便可以将其撤销到适当的颜色。但是我无法做到。任何人都可以帮助。

这是我当前正在使用的代码来获取像素颜色:

UIGraphicsBeginImageContext(self.tempDrawImage.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
int bpr = CGBitmapContextGetBytesPerRow(context);
unsigned char * data = CGBitmapContextGetData(context);
if (data!=NULL)
{
    int offset = bpr*(lastPoint.y)+ 4*(lastPoint.x);
    NSLog(@"Red : %d",data[offset+0]);
    NSLog(@"Green : %d",data[offset+1]);
    NSLog(@"Blue : %d",data[offset+2]);
}

最佳解决方案用于使用uiimage collatpixel类别UIImage类别。我已经使用了这个,非常漂亮和完美。

您可以在图像绘图修改开始之前每次创建图像的副本(例如,在屏幕上的用户点击之前)。

最新更新