title: Most efficient way to draw part of an image in iOS
body: <p>Given an <code>UIImage</code> and a <code>CGRect</code>, what is the most efficient way (in memory and time) to draw the part of the image corresponding to the <code>CGRect</code> (without scaling)?</p>
  
  <p>For reference, this is how I currently do it:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height);    
      CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect);
      CGContextTranslateCTM(context, 0, rect.size.height);
      CGContextScaleCTM(context, 1.0, -1.0);
      CGContextDrawImage(context, rect, imageRef);
      CGImageRelease(imageRef);
  }
  </code></pre>
  
  <p>Unfortunately this seems extremely slow with medium-sized images and a high <code>setNeedsDisplay</code> frequency. Playing with <code>UIImageView</code>'s frame and <code>clipToBounds</code> produces better results (with less flexibility).</p>