Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust a Quartz 2D context to account for a Retina display?

I have a Quartz 2D game which draws directly onto a context. For this reason I am having to adapt the code so that it scales if appropriate for a Retina display. I am doing this using the following code:

- (CGFloat) displayScale
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {

    return [[UIScreen mainScreen]scale];

}

else  

{
    return 1.0;
}

}

What I am now struggling with is how to manipulate my Quartz context in my -drawRect: method to mulitply by the returned scale value. Can anyone help me with this code ?

like image 917
GuybrushThreepwood Avatar asked Dec 28 '22 06:12

GuybrushThreepwood


1 Answers

You don't need to change anything in your Quartz code to account for the Retina display. If the correct contentScaleFactor is set on your UIView or CALayer using code like the following:

if ([view respondsToSelector:@selector(setContentScaleFactor:)])
{
    view.contentScaleFactor = [[UIScreen mainScreen] scale];
}

the 2-D drawing you do within -drawRect: or -drawInContext: will be automatically rendered sharply for the Retina display. Remember that the coordinates you specify for the Quartz drawing will be in points, not pixels. With a scale factor of 2.0 for a Retina display, 1 point = 2 pixels.

See the "Updating Your Custom Drawing Code" section in the iOS Application Programming Guide for more.

like image 171
Brad Larson Avatar answered Jan 18 '23 22:01

Brad Larson