首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGLayer和抗锯齿CGPaths

CGLayer和抗锯齿CGPaths
EN

Stack Overflow用户
提问于 2011-08-06 08:05:11
回答 1查看 2.4K关注 0票数 4

我在一个iPad上的drawRect方法中的Cocoa视图中绘制了几个CGPaths。我一开始直接把它们画到UIGraphicsGetCurrentContext()上下文中,但当我的路径变得很长时,性能就变差了。基于several的其他问题,我开始研究使用CGLayer

因此,我现在要做的就是在调用CGLayerGetContext后得到的CGContextRef中呈现一个路径。下面是我正在做的事情的基本轮廓:

代码语言:javascript
复制
// 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);

现在,我得到了很好的绘画性能,但我的线条非常参差不齐,似乎没有消除锯齿。我试着添加了

代码语言:javascript
复制
CGContextSetShouldAntialias(layerContext, YES);
CGContextSetAllowsAntialiasing(layerContext, YES);
CGContextSetInterpolationQuality(layerContext, kCGInterpolationHigh);

添加到上面的代码中,但没有用。我甚至在主context上设置了抗锯齿属性,没有做任何更改。我截取了相同代码结果的屏幕截图,但第二个图像是从CGLayer中绘图创建的图像。正如你所看到的,它确实参差不齐,尽管是相同的代码,只是绘制到一个layerContext中。如何才能使CGLayer中的线条平滑?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-17 07:03:06

我发现第二个图像没有抗锯齿的原因是因为没有什么可以抗锯齿的。背景是空的,因此抗锯齿效果不起作用。如果在视图的边界上画一个完全透明的大矩形,则绘制的线条将带有抗锯齿效果。然而,性能却被扼杀了。最好不要走这条路。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6963857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档