title: how to get the RGBA value of UIImage in the specific clicked point
body: <p>I am developing an iPad application that has multiple UIImageViews in a viewcontoller. each image has some transparent parts. when the user click on the image I want to test if the area of the image he clicked on was NOT transparent then I want to do some action</p>
  
  <p>after searching I have reached the conclusion that I have to access the rawdata of the image and check the alpha value of the point the user clicked on</p>
  
  <p>I used the solution found in <a href="https://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics">here</a> and it helped a lot. I modified the code so that if the point the user clicked on was transparent (alpha &lt;1) then prent 0 else print 1.  however, the result is not accurate in runtime. I sometimes get 0 where the point clicked is not transparent and viseversa. I think that there is a problem with the <strong>byteIndex</strong> value I am not sure that it returnes the color data at the point the user clicked</p>
  
  <p>here is my code</p>
  
  <pre><code>   CGPoint touchPoint;
      - (void)viewDidLoad
  {
      [super viewDidLoad];
      [logo addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]];
      }
  
  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
  
      UITouch *touch = [[event allTouches] anyObject];
       touchPoint = [touch locationInView:self.view];
  
  }
  - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
  {
  
      int x = touchPoint.x;
      int y = touchPoint.y;
  
      [self getRGBAsFromImage:img atX:x andY:y];
  }
  
  - (void)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy {
  
      // First get the image into your data buffer
      CGImageRef imageRef = [image CGImage];
      NSUInteger width = CGImageGetWidth(imageRef);
      NSUInteger height = CGImageGetHeight(imageRef);
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
      NSUInteger bytesPerPixel = 4;
      NSUInteger bytesPerRow = bytesPerPixel * width;
      NSUInteger bitsPerComponent = 8;
      CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                   bitsPerComponent, bytesPerRow, colorSpace,
                                                   kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
      CGColorSpaceRelease(colorSpace);
  
      CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
      CGContextRelease(context);
  
      // Now your rawData contains the image data in the RGBA8888 pixel format.
      int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
  
  
          CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
          byteIndex += 4;
          if (alpha &lt; 1) {
              NSLog(@"0");
  // here I should add the action I want
  
          }
          else NSLog(@"1");
  
  
  
      free(rawData);
  
  }
  </code></pre>
  
  <p>Thank you in advace </p>