title: UIImage face detection
body: <p>I am trying to write a routine that takes a UIImage and returns a new UIImage that contains just the face. This would seem to be very straightforward, but my brain is having problems getting around the CoreImage vs. UIImage spaces.</p>
  
  <p>Here's the basics:</p>
  
  <pre><code>- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
      CGImageRef sourceImageRef = [image CGImage];
      CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
      UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
      CGImageRelease(newImageRef);
      return newImage;
  }
  
  
  -(UIImage *)getFaceImage:(UIImage *)picture {
    CIDetector  *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                               context:nil 
                                               options:[NSDictionary dictionaryWithObject: CIDetectorAccuracyHigh forKey: CIDetectorAccuracy]];
  
    CIImage *ciImage = [CIImage imageWithCGImage: [picture CGImage]];
    NSArray *features = [detector featuresInImage:ciImage];
  
    // For simplicity, I'm grabbing the first one in this code sample,
    // and we can all pretend that the photo has one face for sure. :-)
    CIFaceFeature *faceFeature = [features objectAtIndex:0];
  
    return imageFromImage:picture inRect:faceFeature.bounds;
  }
  </code></pre>
  
  <p>The image that is returned is from the flipped image. I've tried adjusting <code>faceFeature.bounds</code> using something like this:</p>
  
  <pre><code>CGAffineTransform t = CGAffineTransformMakeScale(1.0f,-1.0f);
  CGRect newRect = CGRectApplyAffineTransform(faceFeature.bounds,t);
  </code></pre>
  
  <p>... but that gives me results outside the image.</p>
  
  <p>I'm sure there's something simple to fix this, but short of calculating the bottom-down and then creating a new rect using that as the X, is there a "proper" way to do this?</p>
  
  <p>Thanks!</p>