在 iPhone 4 上的 CATiledLayer 中绘制时出现模糊像素跨接
我使用以下代码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了:
使用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.