1

I am working on a paint application and want to build the undo manager. I store the pixel co-ordinate value of each location where the user draws but I also want to store the old pixel colour of the point where the user has drawn so that I can undo it to appropriate colour. But I am unable to do so. Can anybody help.

Here is the code I am currently using to get the pixel colour:

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]);
}
Santosh
  • 1,254
  • 2
  • 16
  • 31
  • may be this question helps you http://stackoverflow.com/questions/21028952/how-to-get-the-pixel-color-values-of-custom-image-inside-imageview-ios – Saad Chaudhry Jan 28 '14 at 11:40
  • and pascs answer is too clear – Saad Chaudhry Jan 28 '14 at 11:41
  • But parcs answer still return only 0. – Santosh Jan 28 '14 at 11:48
  • your question and its description both doesn't match `Finding colour of specific pixel on UIImageView` and your code shows that you are getting the specific pixel. Now If you want to draw something on the Image I suggest you to first make A copy And then revert on it when redrawing or undo. – Saad Chaudhry Jan 28 '14 at 13:25

2 Answers2

0

Best solution for this to use UIImage+ColorAtPixel category class of UIImage. I have used this, very nice and perfect.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
0

You can create the copy of your image every time before image drawing modifications begin (for example just before a user taps on the screen).

Miroslav
  • 546
  • 2
  • 9
  • 22