1

我想更改用户触摸图像的颜色。我有一些代码来获取下面的图像数据

NSString * path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"jpg"];
UIImage * img = [[UIImage alloc]initWithContentsOfFile:path];
CGImageRef image = [img CGImage];
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image));
const unsigned char * buffer =  CFDataGetBytePtr(data);

我知道我可以轻松获得接触点,但我的问题如下

  1. 正如我们在视网膜显示 1 点 = 2 像素中所知道的那样,我是否知道需要为单个触摸点更改 2 像素的颜色?如果我在任何地方错了,请纠正我?
  2. 如何从图像数据中获取这两个像素?
4

1 回答 1

1

向呈现图像的 UIImageView 添加手势识别器。当该识别器被触发时,您关心的位置将是......

// self.imageView is where you attached the recognizer.  This == gestureRecognizer.view
CGPoint imageLocation = [gestureRecognizer locationInView:self.imageView];

可以通过确定图像的比例因子来独立将此位置解析为像素定位设备。

要获取图像位置,请将该比例因子应用于手势位置...

CGPoint pixel = CGPointMake(imageLocation.x*image.scale, imageLocation.y*image.scale)

这应该是访问图像的正确坐标。剩下的步骤是获取像素数据。 这篇文章提供了一种看起来合理的方法来做到这一点。(也没有亲自尝试过)。

于 2013-03-03T18:52:51.727 回答