title: iPhone: How to use CGContextConcatCTM for saving a transformed image properly?
body: <p>I am making an iPhone application that loads an image from the camera, and then the user can select a second image from the library, move/scale/rotate that second image, and then save the result. I use two UIImageViews in IB as placeholders, and then apply transformations while touching/pinching.</p>
  
  <p>The problem comes when I have to save both images together. I use a rect of the size of the first image and pass it to <code>UIGraphicsBeginImageContext</code>. Then I tried to use <code>CGContextConcatCTM</code> but I can't understand how it works: </p>
  
  <pre><code>CGRect rect = CGRectMake(0, 0, img1.size.width, img1.size.height); // img1 from camera
  UIGraphicsBeginImageContext(rect.size); // Start drawing
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextClearRect(ctx, rect); // Clear whole thing
  [img1 drawAtPoint:CGPointZero]; // Draw background image at 0,0
  CGContextConcatCTM(ctx, img2.transform); // Apply the transformations of the 2nd image
  </code></pre>
  
  <p>But what do I need to do next? What information is being held in the img2.transform matrix? The documentation for <code>CGContextConcatCTM</code> doesn't help me that much unfortunately..
  Right now I'm trying to solve it by calculating the points and the angle using trigonometry (with the help of <a href="https://stackoverflow.com/questions/1379811/obtaining-the-rotation-and-size-of-a-uiimageview-based-on-its-transformation-matr">this answer</a>), but since the transformation is there, there has to be an easier and more elgant way to do this, right?</p>