在 iPhone 4 上的 CATiledLayer 中绘制时出现模糊像素跨接

发布于 2024-09-26 11:38:34 字数 692 浏览 1 评论 0原文

我使用以下代码在 CATiledLayer 中画一条线:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CGContextMoveToPoint(ctx, 130, 100.5);
    CGContextAddLineToPoint(ctx, 160, 100.5);
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
    CGContextDrawPath(ctx, kCGPathStroke);
}

我得到的是这条 4px 模糊线:

http://img837.imageshack.us/img837/5002/fuzzyline.png

如果我将 CATiledLayer 更改为 CALayer,则线条很清晰,宽度为 2px,正如预期的那样。 我仅在 iPhone 4 上出现此行为,在 3GS 上,CALayer 和 CATiledLayer 上的线条都很尖锐。当然,在 3GS 上,线条是 1px 粗。

知道如何克服这种行为。

I'm drawing a line in a CATiledLayer using the following code:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CGContextMoveToPoint(ctx, 130, 100.5);
    CGContextAddLineToPoint(ctx, 160, 100.5);
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
    CGContextDrawPath(ctx, kCGPathStroke);
}

What I get is this 4px blurry line:

http://img837.imageshack.us/img837/5002/fuzzyline.png

If I change the CATiledLayer to a CALayer, the line is sharp and its width is 2px, as expected.
I get this behavior only on iPhone 4, on 3GS the line is sharp on both CALayer and CATiledLayer. Of course, on 3GS the line is 1px thick.

Any idea how to overcome this behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2024-10-03 11:38:34

我已经找到了:
使用contentsScale == 1.0 创建CATiledLayer。如果将其附加到 contentScaleFactor == 2.0 的视图,图层会放大,这就是我的绘图的混乱之处。

解决方案:
在附加视图之前设置layer.contentsScale = 2.0。

Apple 表示,创建的任何未附加到视图的图层的 contentScale == 1.0,但在我的测试中,创建的 CALayer 的 contentScale == 2。

I've found it:
The CATiledLayer gets created with contentsScale == 1.0. If you attach it to a view with the contentScaleFactor == 2.0, the layer gets enlarged and here is where is screws up my drawing.

Solution:
Set layer.contentsScale = 2.0 before attaching it a view.

Apple says that any layer that is created not attached to a view, has the contentScale == 1.0, but in my test the CALayer is created with contentScale == 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文