title: CGContextDrawImage draws large images very blurry
body: <p>I'm trying to make an object that can draw a portion of a large (say 2048 x 1537) image using CGContextDrawImage(…). It works just fine, except that it is extremely blurry. I'm using a "drawingController" which overrides drawLayer:inContext: like this</p>
  
  <pre><code>-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
      CGImageRef drawImage = CGImageRetain(imageToDraw);
      if(drawImage) {
          CGRect drawRect = CGRectMake(0, 0, 1024, 748);//Just hard coded to my window size for now.
          CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
          CGContextDrawImage(ctx, drawRect, drawImage);
      }
      CGImageRelease(drawImage);
  }
  </code></pre>
  
  <p>And this is how I set up my custom image hosting view:</p>
  
  <pre><code>- (id)initWithFrame:(NSRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          // Initialization code here.
          NSString *filePath = [[NSBundle mainBundle] pathForResource:@"large" ofType:@"jpg"];
          CGImageRef imageRef = [self createCGImageFromFilePath:filePath];
  
  
          drawingController = [[DrawingLayerController alloc] init];
          drawingController.imageToDraw = imageRef;
  
          [self setWantsLayer:YES];
          imageLayer = [[CALayer alloc] init];
          imageLayer.frame = self.bounds;
          imageLayer.drawsAsynchronously = YES;
          imageLayer.delegate = drawingController;
          [self.layer addSublayer:imageLayer];
  
          imageLayer.contentsRect = CGRectMake(0.5, 0.265452179, 0.5, 0.486662329); //Just an example contents rect.
          imageLayer.drawsAsynchronously = YES;
  
          [imageLayer setNeedsDisplay];
          [imageLayer displayIfNeeded];
      }
      return self;
  }
  </code></pre>
  
  <p>Here is a portion of the image produced along side the source image at a similar zoom scale opened in Preview, so you can see just how blurry the image is as compared to the source image.</p>
  
  <p><img src="https://i.stack.imgur.com/EZkHz.png" alt="Hopefully the image doesn&#39;t become blurry when I upload it haha"></p>
  
  <p>As you can see, the image produced by my code looks HORRIBLE. What is up with that?</p>
  
  <p>Also, just for completeness, I have tried using a CATiledLayer for my imageLayer. The resulting image looks just as awful. </p>
  
  <p>One final note: The code needs to be compatible with iOS AND OSX, but I am doing all my testing on OSX.</p>
  
  <p>EDIT: Thanks to @Andrea, I've added the following above CGContextDrawImage(…)</p>
  
  <pre><code>CGFloat scale = MAX(1,MAX(CGImageGetWidth(drawImage)/drawRect.size.width,CGImageGetHeight(drawImage)/drawRect.size.height));
  
  layer.contentsScale = scale;
  </code></pre>