-1

Hello everyone I need help with my iOS app. How can I get the background color of the image (that I insert in UIImageView) exactly where I tap. I create a Tap Gesture Recognizer but I do not know how to read the background color where I tap.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Neoxxx
  • 33
  • 6

1 Answers1

5

add gesture in your control

UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [lblSlider addGestureRecognizer:tapRecognizer];
        lblSlider.userInteractionEnabled = YES;

after add this method

 - (void)tapGesture:(UITapGestureRecognizer *)recognizer
    {
        CGPoint point1 = [recognizer locationInView:recognizer.view];

        UIGraphicsBeginImageContext(recognizer.view.bounds.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [recognizer.view.layer renderInContext:context];

        int bpr = (int)CGBitmapContextGetBytesPerRow(context);
        unsigned char * data = CGBitmapContextGetData(context);
        if (data != NULL)
        {
            int offset = bpr*round(point1.y) + 4*round(point1.x);
            int blue = data[offset+0];
            int green = data[offset+1];
            int red = data[offset+2];
            int alpha =  data[offset+3];

            NSLog(@"%d %d %d %d", alpha, red, green, blue);

            if (alpha == 0)
            {
                // Here is tap out of text
            }
            else
            {
                // Here is tap right into text
            }
        }

        UIGraphicsEndImageContext();
    }
Ankit Kargathra
  • 309
  • 3
  • 16