Note that there are some explanatory texts on larger screens.

plurals
  1. POCGLayer and Anti-aliased CGPaths
    primarykey
    data
    text
    <p>I am drawing several <code>CGPaths</code> in a Cocoa view in the drawRect method on an iPad. I started out drawing them straight to the <code>UIGraphicsGetCurrentContext()</code> context, but performance went south when my paths got really long. Based on <a href="https://stackoverflow.com/questions/2701783/iphone-jagged-edges-when-applying-perspective-to-calayer">several</a> other questions, I started looking into using <code>CGLayer</code>s.</p> <p>So what I do now is to render a path inside of the <code>CGContextRef</code> I get from calling <code>CGLayerGetContext</code>. Here's the basic outline of what I'm doing:</p> <pre class="lang-c prettyprint-override"><code>// rect comes from the drawRect: method layer = CGLayerCreateWithContext(context, rect.size, NULL); layerContext = CGLayerGetContext(layer); CGContextSetLineCap(layerContext, kCGLineCapRound); // Set the line styles CGContextSetLineJoin(layerContext, kCGLineJoinRound); CGContextSetLineWidth(layerContext, 1.0); CGContextSetStrokeColorWithColor(layerContext, [UIColor blackColor].CGColor); // Create a mutable path path = CGPathCreateMutable(); // Move to start point //... for (int i = 0; i &lt; points.count; i++) { // compute controlPoint and anchorPoint //... CGPathAddQuadCurveToPoint(path, nil, controlPoint.x, controlPoint.y, anchorPoint.x, anchorPoint.y); } // Stroke the path CGContextAddPath(layerContext, path); CGContextStrokePath(layerContext); // Add the layer to the main context CGContextDrawLayerAtPoint(context, CGPointZero, layer); </code></pre> <p>Now, I get good performance drawing, but my lines are extremely jagged and do not seem to be anti-aliased. I've tried adding</p> <pre class="lang-c prettyprint-override"><code>CGContextSetShouldAntialias(layerContext, YES); CGContextSetAllowsAntialiasing(layerContext, YES); CGContextSetInterpolationQuality(layerContext, kCGInterpolationHigh); </code></pre> <p>to the code above to no avail. I've even set the anti-aliasing properties on the main <code>context</code>, with no change. I took screen shots of the same code results, but with the second image being the image created from drawing in the <code>CGLayer</code>. As you can see, it is really jagged, despite being the same code, just drawing into a <code>layerContext</code>. How can I get the lines in the <code>CGLayer</code> to be smooth?</p> <p><img src="https://i.stack.imgur.com/bAPc3.png" alt="smooth lines"> <img src="https://i.stack.imgur.com/g1EWk.png" alt="jagged lines"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload