Iducool
Iducool

Reputation: 3663

Change color of pixel based on touch point

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?

Upvotes: 1

Views: 785

Answers (1)

danh
danh

Reputation: 62676

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

Upvotes: 1

Related Questions