CGLayer and Anti-aliased CGPaths

I draw several CGPaths in a Cocoa view in the drawRect method on an iPad. I started drawing them right in the context of UIGraphicsGetCurrentContext() , but the performance went south when my paths became very long. Based on a few other questions, I began to study using CGLayer s.

So, now I have to make the path inside the CGContextRef that I get from the call to CGLayerGetContext . Here is a basic outline of what I'm doing:

 // 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 < 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); 

Now I get a good performance drawing, but my lines are very jagged and seem to be not smooth. I tried adding

 CGContextSetShouldAntialias(layerContext, YES); CGContextSetAllowsAntialiasing(layerContext, YES); CGContextSetInterpolationQuality(layerContext, kCGInterpolationHigh); 

to the above code to no avail. I even set the smoothing properties on the main context unchanged. I took screenshots with the same code results, but with the second image there was an image created from a drawing in CGLayer . As you can see, it is really jagged, despite the fact that it is the same code, just by painting in layerContext . How can I make strings in CGLayer smooth?

smooth linesjagged lines

+4
iphone cocoa ipad quartz-graphics quartz-2d
Aug 6 2018-11-11T00:
source share
1 answer

I found the reason the second image was not smooth because there was nothing against the anti-alias. The background was empty, and so anti-aliasing did not work. If you make a large rectangle over the borders of a fully transparent view, the line will be drawn using smoothing. However, performance is killed. It’s better not to go this route.

+2
Aug 16 2018-11-11T00:
source share



All Articles