简体   繁体   中英

Color pixel with tap recognizer

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM