Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Neoxxx

macrumors newbie
Original poster
Jul 30, 2015
1
0
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.

I try this part of code found on stackoverflow:

in viewDidLoad:

Code:
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];

[self.view addGestureRecognizer:tapRecognizer];

and define:

Code:
- (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();

  }


but i get error on CGBitmapContextGetData

Code:
cannot initialize a variable of type 'unsigned char *' with an r value of type 'void *'

and I do not know how to change it.


thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.