1

I want to change the color where user touch on image. I got some code to get the image data which is below

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);

I know I can easily get the touch point but my questions are below

  1. As we know in retina display 1 point = 2 pixel so, do I know need to change the colour of 2 pixel for single touch point? Please correct me If I am wrong anywhere?
  2. How to get this two pixel from image data?
Iducool
  • 3,543
  • 2
  • 24
  • 45

1 Answers1

1

Add a gesture recognizer to the UIImageView that presents the image. When that recognizer is triggered, the location you care about will be...

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

Resolving this location to a pixel location device independently can be done by determining the scale factor of the image.

To get the image location, apply that scale factor to the gesture location...

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

This should be the correct coordinate for accessing the image. The remaining step is to get the pixel data. This post provides a reasonable-looking way to do that. (Also haven't tried this personally).

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
  • I have done few changes to get the point as per your suggestion. But we are getting image data for pixels. So, to get correct pixel color for certain point we have to do: pixel=CGPointMake(imageLocation.x*image.scale, imageLocation.y*image.scale). – Iducool Mar 08 '13 at 16:28
  • If my comment seems appropriate the please edit your answer as per that so I will accept it. – Iducool Mar 08 '13 at 16:28
  • happy to edit, but not sure I understand - your UIImage has a property called 'scale'? – danh Mar 08 '13 at 16:40
  • Yes. UIImage has built in property scale. – Iducool Mar 08 '13 at 16:43
  • wow. don't know why i've never seen that before! sure, will edit right away. that's a good tip for me and whomever finds this post. – danh Mar 08 '13 at 16:44