Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X: Best way to do runtime check for retina display?

Given a Cocoa application which runs on Mac OS X 10.7 and later:

What is the best way to check, at runtime, if your app is currently running on a Mac with at least one retina display attached?

If checking for this sort of thing is just really wrong-headed, I fully welcome a well-reasoned explanation of why (and I will up-vote such answers if they are good).

But I'd still like to know :).

It seems likely you could just do a check specifically for the new Mac Book Pro "Retina" hardware (the only Mac which currently has a retina display), but I'd really prefer a more general/generic/future-proof way to check than this.

Ideally, I'd like to know how to detect the retina display, not the specific Mac model which currently happens to ship with a retina display.

like image 539
Todd Ditchendorf Avatar asked Jun 16 '12 21:06

Todd Ditchendorf


People also ask

How do you know if your MacBook has Retina display?

Go to Apple logo (top left) > About this Mac. Click on Overview in the panel which comes up and the third line down Macbook Pro (retina). should confirm it.

What is difference between Retina display and normal?

The pixel density of Retina displays is so high that your eyes can't detect individual pixels at a normal viewing distance. This gives content incredible detail and dramatically improves your viewing experience.

What PPI is Retina?

With Retina, Apple squeezed four times as many pixels into the same space, creating a density of 326 pixels per inch (ppi). When viewing at 10-12 inches people stop seeing individual pixels at densities of around 300 ppi, making Retina displays smooth and crisp.

What DPI is Retina?

A Mac's Retina display could be 250 DPI; an iPhone, iPad or modern Android device may well be over 300 DPI. The displays are still quite small, however, so providing a double-sized image for higher-resolution screens is usually all that's needed to make a website look good on all modern devices.


Video Answer


3 Answers

If you really do need to do this, take a look at -[NSScreen backingScaleFactor]. But, this does seem wrong-headed, at least without knowing more about why you want to know.

While there is currently only one Mac with a Retina display, there may eventually be standalone displays that support Retina (and can be attached/detached at runtime) and you may be able to configure the built-in Retina display in 1x mode. Thus the answer to the question "is there a Retina display attached" can change at any time.

Rather, you may want to know if your content should be drawn with a given scale by using the -convert*ToBacking: methods or -[NSWindow backingScaleFactor]. For a ton more detail, watch the WWDC 2012 session video "Advanced Tips and Tricks for High Resolution on OS X" (when posted, hopefully within the next few weeks).

like image 93
tjw Avatar answered Oct 05 '22 20:10

tjw


I just ran into an instance where I needed to detect if any screen was high resolution, this worked

float displayScale = 1;
    if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
        NSArray *screens = [NSScreen screens];
        NSUInteger screenCount = screens.count
        for (int i = 0; i < screenCount; i++) {
            float s = [screens[i] backingScaleFactor];
            if (s > displayScale)
                displayScale = s;
        }
    }
like image 42
nuclearnova Avatar answered Oct 05 '22 20:10

nuclearnova


Well, on iOS you use the UIScreen.scale property. If it returns 2.0, then you're on a device with a high resolution display. Otherwise you're on a low-resolution device.

So I'd imagine that on Mac OS you could use either -[NSScreen backingScaleFactor] or -[NSWindow backingScaleFactor].

like image 42
Dave DeLong Avatar answered Oct 05 '22 20:10

Dave DeLong