title: scale and crop image ios
body: <p>I'm working in a function for scaling and crop image (similar to camara app) for iOS, the code below works fine, just that the resulting image came up side down, and I would like to understand why.</p>
  
  <p>Thanks </p>
  
  <pre><code>- (UIImage*)imageByCropping:(UIImageView *)imageViewToCrop toRect:(CGRect)rect
  {
      //create a context to do our clipping in
      CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
      UIImage *imageToCrop = imageViewToCrop.image;
      UIGraphicsBeginImageContext(newRect.size);
      CGContextRef currentContext = UIGraphicsGetCurrentContext();
  
      //create a rect with the size we want to crop the image to
      //the X and Y here are zero so we start at the beginning of our
      //newly created context
      CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
      CGContextClipToRect( currentContext, clippedRect);
  
  
      //draw the image to our clipped context using our offset rect
      CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);
  
      //pull the image from our cropped context
      UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
  
      //pop the context to get back to the default
      UIGraphicsEndImageContext();
  
      return cropped;
  
  } 
  </code></pre>