title: Why the PNG image resized and shows very well in UIImageView but quality not good when I draw it by CGContextDrawImage
body: <p>I have a PNG (52x52) image file,if I show it in a UIImageView (16x16) , it is showed good.</p>
  
  <p>But if I try to use CGContextDrawImage to draw, the quality is very bad.</p>
  
  <p>Please see below detail code:</p>
  
  <p>resizeImage is used to resize the image (copy from apple site).
  drawStonesPng is do draw the image and called form CALayer:: drawInContext</p>
  
  <pre><code>- (UIImage*)resizeImage:(UIImage*)image toWidth:(NSInteger)width height:(NSInteger)height
  {
      // Create a graphics context with the target size
      // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
      // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
      CGSize size = CGSizeMake(width, height);
      if (NULL != UIGraphicsBeginImageContextWithOptions)
          UIGraphicsBeginImageContextWithOptions(size, NO, 0);
      else
          UIGraphicsBeginImageContext(size);
  
      CGContextRef context = UIGraphicsGetCurrentContext();
  
      // Flip the context because UIKit coordinate system is upside down to Quartz coordinate system
      CGContextTranslateCTM(context, 0.0, height);
      CGContextScaleCTM(context, 1.0, -1.0);
  
      // Draw the original image to the context
      CGContextSetBlendMode(context, kCGBlendModeCopy);
      CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), image.CGImage);
  
      // Retrieve the UIImage from the current context
      UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
  
      UIGraphicsEndImageContext();
  
      return imageOut;
  }
  
  
  
  -(void)drawStonesPng:(CGContextRef)ctx{
  
      _cellWidth=16;
      float x1,y1;
      float f1=0.48;
  
     // self.contentsScale = [UIScreen mainScreen].scale;
  
      UIImage* resizedImageBlack = [self resizeImage:[UIImage imageNamed:@"blackstone52"] toWidth:_cellWidth*f1*2 height:_cellWidth*f1*2];
      UIImage* resizedImageBlackShadow = [self resizeImage:[UIImage imageNamed:@"blackshadow"] toWidth:_cellWidth*f1*2 height:_cellWidth*f1*2];
      CGImageRef imgBlack = [resizedImageBlack CGImage];
      CGImageRef imgBlackShadow = [resizedImageBlackShadow CGImage];
      CGFloat width = CGImageGetWidth(imgBlack), height = CGImageGetHeight(imgBlack);
  
  
  //    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
  //    CGContextSetShouldAntialias(ctx, true);
      for(int y=0; y&lt;_boardSize; y++) {
          for(int x=0; x&lt;_boardSize; x++) {
  
              STONE_T stone = [MyGoController getStoneType:x y:y];
              if(STONE_INVALID==stone){
                  CGContextClosePath(ctx);
                  return;
              }
  
              if(stone==STONE_BLACK){
  
                  x1=(x+1)*_cellWidth-_cellWidth*f1;
                  y1=(y+1)*_cellWidth-_cellWidth*f1;
  
  
  
  //                CGFloat scale = _cellWidth*0.9/width;
  //                NSLog(@"Scale: %f\nWidth: %f\nHeight: %f", scale, width, height);
  //                CGContextTranslateCTM(ctx, 0, height / scale);
  //                CGContextScaleCTM(ctx, 1.0, -1.0);
  
  
                  CGFloat scale = [[UIScreen mainScreen] scale];
  
                  self.contentsScale =[[UIScreen mainScreen] scale];
  
                  NSLog(@"Scale: %f\nWidth: %f\nHeight: %f", scale, width, height);
                  CGContextTranslateCTM(ctx, 0, width / scale);
                  CGContextScaleCTM(ctx, 1.0, -1.0);
  
  
                  CGContextDrawImage(ctx, CGRectMake(x1+1, y1+1, width/scale,width/scale), imgBlackShadow);
                  CGContextDrawImage(ctx, CGRectMake(x1, y1, width/scale,width/scale), imgBlack);
  
  
  
              }
  
          }
      }
  
      resizedImageBlack = [self resizeImage:[UIImage imageNamed:@"whitestone52"] toWidth:_cellWidth*f1*2 height:_cellWidth*f1*2];
      resizedImageBlackShadow = [self resizeImage:[UIImage imageNamed:@"whiteshadow"] toWidth:_cellWidth*f1*2 height:_cellWidth*f1*2];
      imgBlack = [resizedImageBlack CGImage];
      imgBlackShadow = [resizedImageBlackShadow CGImage];
  
      //draw white stones
  
      for(int y=0; y&lt;_boardSize; y++) {
          for(int x=0; x&lt;_boardSize; x++) {
              STONE_T stone = [MyGoController getStoneType:x y:y];
  
              if(stone==STONE_WHITE){
  
                  x1=(x+1)*_cellWidth-_cellWidth*f1;
                  y1=(y+1)*_cellWidth-_cellWidth*f1;
  
  
  
                  //                CGFloat scale = _cellWidth*0.9/width;
                  //                NSLog(@"Scale: %f\nWidth: %f\nHeight: %f", scale, width, height);
                  //                CGContextTranslateCTM(ctx, 0, height / scale);
                  //                CGContextScaleCTM(ctx, 1.0, -1.0);
  
  
                  CGContextDrawImage(ctx, CGRectMake(x1+1, y1+1, _cellWidth*f1*2,_cellWidth*f1*2), imgBlackShadow);
                  CGContextDrawImage(ctx, CGRectMake(x1, y1, _cellWidth*f1*2,_cellWidth*f1*2), imgBlack);
  
  
  
  
  
              }
  
          }
      }
  
  }
  </code></pre>