title: UIImageView pinch zoom and reposition to fit overlay
body: <p>I am using an UIImagePickerView to grab photos from the camera or camera roll.  When the user 'picks' an image, I'm inserting the image onto an UIImageView, which is nested in a UIScrollView to allow pinch/pan. I have an overlay above the image view which represents the area to which the image will be cropped (just like when UIImagePickerView's .allowEditing property is YES).  </p>
  
  <p>The Apple-provided "allowEditing" capability also has the same problem I'm seeing with my code (which I why I tried to write it myself in the first place, and I need custom shapes in the overlay).  The problem is that I can't seem to find a good way to allow the user to pan around over ALL the image.  There are always portions of the image which can't be placed in the crop window.  It's always content around the edges (maybe the outside 10%) of the image, which cannot be panned into the crop window.</p>
  
  <p><img src="https://i.stack.imgur.com/W2cy8.png" alt="unusable crop area"></p>
  
  <p>In the above photo, the brown area at the top and bottom are the scroll view's background color.  The image view is sized to the image.</p>
  
  <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  
      UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  
      // Calculate what height/width we'll need for our UIImageView.
      CGSize screenSize = [UIScreen mainScreen].bounds.size;
      float width = 0.0f, height = 0.0f;
      if (image.size.width &gt; image.size.height) {
          width = screenSize.width;
          height =  image.size.height / (image.size.width/screenSize.width);
      } else {
          height = screenSize.height;
          width = image.size.width / (image.size.height/screenSize.height);
      }
  
      if (width &gt; screenSize.width) {
          height /= (width/screenSize.width);
          width = screenSize.width;
      }
      if (height &gt; screenSize.height) {
          width /= (height/screenSize.height);
          height = screenSize.height;
      }
  
      // Update the image view to the size of the image and center it.
      // Image view is a subview of the scroll view.
      imageView.frame = CGRectMake((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
      imageView.image = image;
      // Setup our scrollview so we can scroll and pinch zoom the image!
      imageScrollView.contentSize = CGSizeMake(screenSize.width, screenSize.height);
  
      // Close the picker.
      [[picker presentingViewController] dismissViewControllerAnimated:YES completion:NULL];
  }
  </code></pre>
  
  <p>I've considered monitoring scroll position and zoom level of the scroll view and disallow a side of the image to pass into the crop "sweet spot" of the image.  This seems like over-engineering, however.  </p>
  
  <p>Does anyone know of a way to accomplish this?</p>