title: AVFoundation photo size and rotation
body: <p>I'm having a nightmare time trying to correct a photo taken with <code>AVFoundation</code> <code>captureStillImageAsynchronouslyFromConnection</code> to size and orient to exactly what is shown on the screen.</p>
  
  <p>I show the <code>AVCaptureVideoPreviewLayer</code> with this code to make sure it displays the correct way up at all rotations:</p>
  
  <pre><code>previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
  [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  previewLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
  if ([[previewLayer connection] isVideoOrientationSupported])
  {
      [[previewLayer connection] setVideoOrientation:(AVCaptureVideoOrientation)[UIApplication sharedApplication].statusBarOrientation];
  }
  [self.view.layer insertSublayer:previewLayer atIndex:0];
  </code></pre>
  
  <p>Now when I have a returned image it needs cropping as it's much bigger than what was displayed.
  I know there are loads of <code>UIImage</code> cropping examples, but the first hurdle I seem to have is finding the correct <code>CGRect</code> to use. When I simply crop to <code>self.view.frame</code> the image is cropped at the wrong location.</p>
  
  <p>The preview is using <code>AVLayerVideoGravityResizeAspectFill</code> and I have my <code>UIImageView</code> also set to <code>AspectFill</code></p>
  
  <p>So how can I get the correct frame that <code>AVFoundation</code> is displaying on screen from the preview layer?</p>
  
  <p>EDIT ----</p>
  
  <p>Here's an example of the problem i'm facing. Using the front camera of an iPad Mini, the camera using the resolution 720x1280 but the display is 768x0124. The view displays this (See the dado rail at the top of the image:</p>
  
  <p><img src="https://i.stack.imgur.com/OyQKi.png" alt="enter image description here"></p>
  
  <p>Then when I take the image and display it, it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/vvCv2.png" alt="enter image description here"></p>
  
  <p>Obviously the camera display was centred in the view, but the cropped image is taken from the top(none seen) section of the photo.</p>