CALayer and pixel alignment
CALayer and pixel alignment
- Subject: CALayer and pixel alignment
- From: Gideon King <email@hidden>
- Date: Wed, 22 Dec 2010 10:09:04 +1000
I am drawing some CALayers, and use code like what I have below so I can draw exactly on pixels.
When I draw it in my CALayer drawing code, it's not always pixel aligned. It's certainly better than if I had done no rounding, but still it is not correct. Is there something I'm not doing right? Why I think it may be CALayer based is because when I draw a whole bunch of these squares on layers, on some layers they are all aligned, and on other layers they are not - they are all affected at the same time. I do not have any scale transforms anywhere - only translations.
CGContextSaveGState(gcontext);
CGRect rect = CGRectMake(55.0f, -2.5f, 5.0f, 5.0f);
CGRect deviceRect = CGContextConvertRectToDeviceSpace(gcontext, rect);
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
deviceRect.origin.x = [[(NSDecimalNumber *)[NSDecimalNumber numberWithFloat:deviceRect.origin.x + 0.5f] decimalNumberByRoundingAccordingToBehavior:handler] floatValue] - 0.5f;
deviceRect.origin.y = [[(NSDecimalNumber *)[NSDecimalNumber numberWithFloat:deviceRect.origin.y + 0.5f] decimalNumberByRoundingAccordingToBehavior:handler] floatValue] - 0.5f;
deviceRect.size.width = [[(NSDecimalNumber *)[NSDecimalNumber numberWithFloat:deviceRect.size.width] decimalNumberByRoundingAccordingToBehavior:handler] floatValue];
deviceRect.size.height = [[(NSDecimalNumber *)[NSDecimalNumber numberWithFloat:deviceRect.size.height] decimalNumberByRoundingAccordingToBehavior:handler] floatValue];
CGRect drawingRect = CGContextConvertRectToUserSpace(gcontext, deviceRect);
CGContextSetRGBStrokeColor(gcontext, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextSetRGBFillColor(gcontext, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextAddRect(gcontext, drawingRect);
CGContextDrawPath(gcontext, kCGPathFillStroke);
CGContextRestoreGState(gcontext);
Regards
Gideon
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden