fuzzy pixel straddling when drawing in a CATiledLayer on iPhone 4
I'm drawing a line in a CATiledLayer using the follow开发者_如何学运维ing 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.
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.
精彩评论