• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
CALayer and pixel alignment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: CALayer and pixel alignment
      • From: Seth Willits <email@hidden>
  • Prev by Date: Vertical Asian Text in Cocoa
  • Next by Date: UIWebview: scrolling vs anchors - followup
  • Previous by thread: Re: Vertical Asian Text in Cocoa
  • Next by thread: Re: CALayer and pixel alignment
  • Index(es):
    • Date
    • Thread