在CALayer中进行抗锯齿绘制

kal*_*rin 5 cocoa-touch core-animation ios

问候!我正在尝试在CALayer中绘制一系列圆圈,这些圆圈位于可缩放的UISCrollView中.这是放大捏的图层.如果我使用CAShapeLayer绘制圆圈,那么它们可以精美地缩放:

CAShapeLayer plotLayer = [CAShapeLayer layer];
plotLayer.bounds = self.bounds;
plotLayer.anchorPoint = CGPointZero;
plotLayer.position = CGPointZero;

CGMutablePathRef path = CGPathCreateMutable();
for (id one in many) {
    CGRect ellipseRect = [one circleRect];
    CGPathAddEllipseInRect(path, NULL, ellipseRect);
}
plotLayer.path = path
CFRelease(path);
[self.layer addSublayer:plotLayer];
[plotLayer setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试在我的drawLayer:inContext:方法中使用香草核心图形绘制它们时,圆圈变得非常锯齿(彻头彻尾的专利!)没有任何数量的antialias jiggery-pokery似乎有帮助:

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    CGContextClip(context);

    for (id one in many) {
        CGRect ellipseRect = [one circleRect];
        CGContextStrokeEllipseInRect(context, ellipseRect);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想弄清楚CAShapeLayer正在做些什么来获得如此好的抗锯齿效果(除了我自己的启发)我可以充分利用我的绘图中的核心图形,而不仅仅是我可以用CAShapeLayer获得的笔触/填充.我只是在某个地方错过了一个变换?

非常感谢!

kal*_*rin 2

简短回答:内容规模

更长的答案:(几乎逐字取自pe8ter at Vector 就像可缩放 UIScrollView 的绘图):

缩放后,您可以通过在相关图层上设置 contentScale 属性来获得更好的渲染效果:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
                       withView:(UIView *)view
                        atScale:(float)scale
{
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithBool:YES] 
                     forKey:kCATransactionDisableActions];
    uglyBlurryTextLayer.contentsScale = scale;
    [CATransaction commit];
}
Run Code Online (Sandbox Code Playgroud)

Brad Larson 在 So a CALayer does not contains a content bitmap of a view? 有一个(通常非常出色的)描述,描述了实际绘图发生的时间(剧透,尽管应用了多少次变换,它通常只发生一次)。基于此,我只能假设设置contentsScale会导致图层再次渲染。