title: Properly crop an image obtained from the photo library
body: <p>I've been working on this all day, and have looked at lots of questions here on SO and google, but so far I can't come up with anything quite right.</p>
  
  <p>I have taken a photo on an iPad running iOS 5.1.1 and cropped it using the Photos app.  I then get a reference to it from the assets library and am getting the full resolution image which is un-cropped.</p>
  
  <p>I've found that the cropping information is contained in the <code>AdjustmentXMP</code> key of <code>metadata</code> on my <code>ALAssetRepresentation</code> object.</p>
  
  <p>So I crop the photo using the XMP info and here is what I get:</p>
  
  <p>Original Photo (1,936 x 2,592):<br>
  <img src="https://i.stack.imgur.com/J0QrS.png" alt="Original Photo">  </p>
  
  <p>Properly Cropped Photo, as seen in the Photos App (1,420 x 1,938):<br>
  <img src="https://i.stack.imgur.com/pbnHv.png" alt="Properly Cropped Photo">  </p>
  
  <p>Photo Cropped With Code Below<br>
  (also 1,420 x 1,938 but cropped roughly 200 pixels too far to the right):<br>
  <img src="https://i.stack.imgur.com/Gu1j7.png" alt="Problem"></p>
  
  <p>This is the XMP data from the photo:</p>
  
  <pre><code>&lt;x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0"&gt;
     &lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"&gt;
        &lt;rdf:Description rdf:about=""
              xmlns:aas="http://ns.apple.com/adjustment-settings/1.0/"&gt;
           &lt;aas:AffineA&gt;1&lt;/aas:AffineA&gt;
           &lt;aas:AffineB&gt;0&lt;/aas:AffineB&gt;
           &lt;aas:AffineC&gt;0&lt;/aas:AffineC&gt;
           &lt;aas:AffineD&gt;1&lt;/aas:AffineD&gt;
           &lt;aas:AffineX&gt;-331&lt;/aas:AffineX&gt;
           &lt;aas:AffineY&gt;-161&lt;/aas:AffineY&gt;
           &lt;aas:CropX&gt;0&lt;/aas:CropX&gt;
           &lt;aas:CropY&gt;0&lt;/aas:CropY&gt;
           &lt;aas:CropW&gt;1938&lt;/aas:CropW&gt;
           &lt;aas:CropH&gt;1420&lt;/aas:CropH&gt;
        &lt;/rdf:Description&gt;
     &lt;/rdf:RDF&gt;
  &lt;/x:xmpmeta&gt;
  </code></pre>
  
  <p>Here is the code that I am using to crop the photo:</p>
  
  <pre><code>ALAssetRepresentation *rep = // Get asset representation
  CGImageRef defaultImage = [rep fullResolutionImage];
  
  // Values obtained from XMP data above:
  CGRect cropBox = CGRectMake(0, 0, 1938, 1420);
  CGAffineTransform transform = CGAffineTransformMake(1, 0, 0, 1, 331, 161);
  
  // Apply the Affine Transform to the crop box:
  CGRect transformedCropBox =  CGRectApplyAffineTransform(cropBox, transform);
  
  // Created a new cropped image:
  CGImageRef croppedImage = CGImageCreateWithImageInRect(defaultImage, transformedCropBox);
  
  // Create the UIImage:
  UIImage *image = [UIImage imageWithCGImage:croppedImage scale:[rep scale] orientation:[rep orientation]];
  
  CGImageRelease(croppedImage);
  </code></pre>
  
  <p>I've reproduced the problem with multiple images. If I just use the <code>fullScreenImage</code> it displays perfectly, but I need the full size image.</p>