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 protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]