CGLayer和抗锯齿CGPath [英] CGLayer and Anti-aliased CGPaths

查看:335
本文介绍了CGLayer和抗锯齿CGPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPad上的drawRect方法的Cocoa视图中绘制了几个 CGPaths 。我开始将它们直接绘制到 UIGraphicsGetCurrentContext()上下文中,但是当我的路径很长时,性能向下。根据几个其他问题,我开始研究使用 CGLayer s。

I am drawing several CGPaths in a Cocoa view in the drawRect method on an iPad. I started out drawing them straight to the UIGraphicsGetCurrentContext() context, but performance went south when my paths got really long. Based on several other questions, I started looking into using CGLayers.

现在我做的是在 CGContextRef 我从调用 CGLayerGetContext 。下面是我所做的基本概要:

So what I do now is to render a path inside of the CGContextRef I get from calling CGLayerGetContext. Here's the 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 good performance drawing, but my lines are extremely jagged and do not seem to be anti-aliased. I've tried adding

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

到上面的代码无效。我甚至在主要的上下文上设置了抗锯齿属性,没有改变。我拍了相同的代码结果的屏幕截图,但第二个图像是从绘制在 CGLayer 中创建的图像。你可以看到,它是真正的锯齿,尽管是相同的代码,只是绘制一个 layerContext 。我如何获得 CGLayer 中的行是平滑的?

to the code above to no avail. I've even set the anti-aliasing properties on the main context, 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 CGLayer. As you can see, it is really jagged, despite being the same code, just drawing into a layerContext. How can I get the lines in the CGLayer to be smooth?


推荐答案

我发现第二个图像没有抗锯齿的原因是因为没有反对别名。背景是空的,所以反锯齿没有工作。如果你在一个完全透明的视图的边界上做一个大矩形,那么这条线将会用反锯齿进行绘制。但是,性能被杀死。最好不要走这条路线。

I found the reason the second image is not anti-aliased is because there was nothing to anti-alias against. The background was empty, and so anti-aliasing didn't work. If you make a big rectangle over the bounds of the view that is entirely transparent, the line will draw with anti-aliasing. However, performance is killed. Better not to go this route.

这篇关于CGLayer和抗锯齿CGPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆