title: UIImageView image crop based on UIView mask
body: <p>So i have an canvas (UIView) and a UIImageView, the canvas acts as a mask over the imageview</p>
  
  <p><img src="https://i.stack.imgur.com/F8FQH.jpg" alt="enter image description here"></p>
  
  <p>i am using UIGestureRecognizers to zoom and rotate the UIImageView which is under the canvas. </p>
  
  <p>i want to convert the final image (show in the canvas to a UIImage, one solution is to convert  the canvas to an image like below </p>
  
  <pre><code>UIGraphicsBeginImageContext(self.canvas.bounds.size);
  [self.canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *newCombinedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  </code></pre>
  
  <p>now this works fine but the problem with this solution is the image is cropped to the dimensions of the canvas so the resolution is very low. </p>
  
  <p>another option i explored was to use some custom UIImage categories to rotate and scale.</p>
  
  <pre><code>[[[self.photoImage image] imageRotatedByDegrees:rotaton_angle] 
       imageAtRect:CGRectMake(x,y width,height)]
  </code></pre>
  
  <p>i need to provide rotation angle (the rotation angle provided by UIGesture Delegate is not in Degrees or Radians, then there is x,y,width,height, i imagine these needs to be calculated based on some scale, (i do get scale value from UIGesture delegate but they do not seem to be correct for this function)</p>
  
  <p>there are a number of solutions here, that guides you to crop and image given a rect. but in my case the rect is not the same scale as the image also there is rotation involved.</p>
  
  <p>any help will be appreciated.  </p>