title: Identify a percentage of transparent pixels in an area of UIImageView
body: <p>I'm trying to set up a collision type hit test for a defined of pixels within a UIImageView. I'm only wish to cycle through pixels in a defined area.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>- (BOOL)cgHitTestForArea:(CGRect)area {
      BOOL hit = FALSE;
  
      CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  
      float areaFloat = ((area.size.width * 4) * area.size.height);
      unsigned char *bitmapData = malloc(areaFloat);    
  
      CGContextRef context = CGBitmapContextCreate(bitmapData,
                                                   area.size.width,
                                                   area.size.height,
                                                   8,
                                                   4*area.size.width,
                                                   colorspace,
                                                   kCGImageAlphaPremultipliedLast);
      CGContextTranslateCTM(context, -area.origin.x, -area.origin.y);
      [self.layer renderInContext:context];
  
      //Seek through all pixels.    
      float transparentPixels = 0;
      for (int i = 0; i &lt; (int)areaFloat ; i += 4) {
          //Count each transparent pixel.
          if (((bitmapData[i + 3] * 1.0) / 255.0) == 0) {
              transparentPixels += 1;
          }
      }
      free(bitmapData);
  
      //Calculate the percentage of transparent pixels. 
      float hitTolerance = [[self.layer valueForKey:@"hitTolerance"]floatValue];
  
      NSLog(@"Apixels: %f hitPercent: %f",transparentPixels,(transparentPixels/areaFloat));
  
      if ((transparentPixels/(areaFloat/4)) &lt; hitTolerance) {
          hit = TRUE;
      }    
  
      CGColorSpaceRelease(colorspace);
      CGContextRelease(context);
  
      return hit;    
  }
  </code></pre>
  
  <p>Is someone able to offer any reason why it isn't working?</p>