title: Painting on CGContextRef on Retina display
body: <p>In my application i have one main view (with its UIGraphicsGetCurrentContext()) and several other CGContextRef canvases. All of them are the same size, the size of the screen - without scale factor.</p>
  
  <p>I draw on the secondary canvases and at the end use CGBitmapContextCreateImage on the secondary canvases and CGContextDrawImage to draw on the main canvas.</p>
  
  <p>On retina displays the result is poor and all lines look pixelated.
  How should i handle painting on retina displays?</p>
  
  <p>The following is the code i use to draw a context to the main context:</p>
  
  <pre><code>void CocoaGraphicsContext::drawContext(GraphicsContext* context, double x, double y, Box* cropBox, Matrix3D* matrix)
  {
      CGAffineTransform matInverted;
  
      if (matrix != NULL)
      {
          CGAffineTransform transMat = CGAffineTransformMake(matrix-&gt;vx1, matrix-&gt;vy1, matrix-&gt;vx2, matrix-&gt;vy2, matrix-&gt;tx, matrix-&gt;ty);
          matInverted = CGAffineTransformInvert(transMat);
  
          CGContextConcatCTM(_cgContext, transMat);
      }
  
      CGImageRef cgImage = CGBitmapContextCreateImage(((CocoaGraphicsContext*)context)-&gt;cgContext());
  
      CGContextSaveGState(_cgContext);
      CGContextSetInterpolationQuality(_cgContext, kCGInterpolationNone);
  
      bool shouldCrop = ((cropBox != NULL) &amp;&amp; (cropBox-&gt;valid()));
  
      if (shouldCrop)
      {
          CGContextClipToRect(_cgContext, CGRectMake(cropBox-&gt;x(), cropBox-&gt;y(), cropBox-&gt;width(), cropBox-&gt;height()));
      }
  
      CGContextDrawImage(_cgContext, CGRectMake(x, y, context-&gt;width(), context-&gt;height()), cgImage);
  
      CGContextRestoreGState(_cgContext);
  
      CGImageRelease(cgImage);
  
      if (matrix != NULL)
      {
          CGContextConcatCTM(_cgContext, matInverted);
      }
  }
  </code></pre>
  
  <p>The main context is taken using UIGraphicsGetCurrentContext() from a UIView class</p>
  
  <p>---- EDITED --- 
  Initialization code:</p>
  
  <pre><code>void CocoaBitmapContext::init(double width, double height, unsigned int* data)
  {
      int bytesPerRow = width * 2  * VT_BITMAP_BYTES_PER_PIXEL;
  
      if (data == NULL)
      {
          _dataOwner = true;
          _data = (unsigned int*)malloc(bytesPerRow * height * 2);
      }
      else
      {
          _dataOwner = false;
          _data = data;
      }
  
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  
      _cgContext = CGBitmapContextCreate(_data,
                                         width * 2,
                                         height * 2,
                                         VT_BITMAP_BITS_PER_COMPONENT,
                                         bytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
  
      CGContextConcatCTM(_cgContext, CGAffineTransformMakeScale([UIScreen mainScreen].scale, [UIScreen mainScreen].scale));
  
      CGContextSetShouldAntialias(_cgContext, false);
      CGContextSetRGBStrokeColor(_cgContext, 0.0, 0.0, 0.0, 0.0);
  
      CGColorSpaceRelease(colorSpace);
  }
  </code></pre>