平滑 Core Graphics 曲线

6
我正在尝试创建一个圆形加载条,并想使用Core Graphics而不是遮罩图像来创建效果。但是,我没有得到我所希望的保真度:enter image description here 无论我尝试了什么,锯齿似乎都很严重。抗锯齿已经开启,并且我已经尝试将“flatness”降低到CGContextRef的0.1。该图像来自(Retina)模拟器,在设备上看起来稍微好一些,但仍然不是很好。
绘画代码在CALayer的子类中:
-(void)drawInContext:(CGContextRef)ctx {    
    CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

    CGFloat delta = toRadians(360 * percent);

    CGFloat innerRadius = 69.5;
    CGFloat outerRadius = innerRadius + 12;

    CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:99/256.0 green:183/256.0 blue:70/256.0 alpha:.5].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithRed:99/256.0 green:183/256.0 blue:70/256.0 alpha:10.0].CGColor);
    CGContextSetLineWidth(ctx, 1);

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathAddRelativeArc(path, NULL, center.x, center.y, innerRadius, -(M_PI / 2), delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, outerRadius, delta - (M_PI / 2), -delta);
    CGPathAddLineToPoint(path, NULL, center.x, center.y-innerRadius);

    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    CGContextAddPath(ctx, path);
    CGContextStrokePath(ctx);
}

这是Core Graphics所能做到的最好吗,还是我漏掉了什么?


1
考虑到您的图像在每个维度上都被缩放了2倍,并且您从Retina模拟器中获取了该图像,我会说您的图层变换或其父级之一存在问题。您是在其他方法中创建ctx还是框架将其传递给您? NSLog(“%@”,[[[UIApplication sharedApplication] keyWindow] recursiveDescription])的输出是什么? - rob mayoff
上下文由CALayer提供给我。这里是recursiveDescription的输出。KDGoalBar是UIControl子类,它被放置在其中,而KDGoalBarPercentLayer则是实际绘图的地方。 - Kevin
1个回答

11

发现了我的具体问题。由于我正在创建一个CALayer(作为子层添加),因此contentScale没有自动设置为Retina显示。这解决了:

percentLayer.contentsScale = [UIScreen mainScreen].scale;

感谢Rob的帮助,让我走上了正确的道路。


我希望我能够给它点赞加十。 - coneybeare
缩放是像我这样的新手的陷阱,当我在非Retina屏幕上预览绘图代码并在Retina屏幕上部署时看到锯齿边缘。您的“scale”一词立即提醒我,我正在非Retina环境中绘制,并期望在Retina环境中完全相同。+1给你。 - Kent Liau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接