简体   繁体   中英

How to programmatically determine native pixel resolution of Retina MacBook Pro screen on OS X?

Given a CGDirectDisplayID returned from

CGError error = CGGetActiveDisplayList(8, directDisplayIDs, &displayCount);

for the built-in screen on a Retina MacBook Pro, I would expect to fetch the native pixel dimensions using

size_t pixelWidth = CGDisplayPixelsWide(directDisplayID);
size_t pixelHeight = CGDisplayPixelsHigh(directDisplayID);

However, these calls only return the dimensions of the currently selected mode. If I change screen resolution I get back different values. I was looking to get back 2880 x 1800 on a 15" rMBP.

How do I fetch the native pixel dimensions of a Retina MacBook Pro screen?

I think the best approach is to enumerate all of the display modes (including the 1x modes) and find the biggest 1x mode's dimensions.

You would use CGDisplayCopyAllDisplayModes() and pass a dictionary with the key kCGDisplayShowDuplicateLowResolutionModes mapped to kCFBooleanTrue as the options to get all of the modes. You can test that CGDisplayModeGetPixelWidth() is equal to CGDisplayModeGetWidth() to determine which are 1x.

CGDisplayModeGetIOFlags can tell you some information of the display. The native resolutions have kDisplayModeNativeFlag set. The following will set ns to be the native resolution of the current screen of the window win .

CGDirectDisplayID sid = ((NSNumber *)[win.screen.deviceDescription
    objectForKey:@"NSScreenNumber"]).unsignedIntegerValue;
CFArrayRef ms = CGDisplayCopyAllDisplayModes(sid, NULL);
CFIndex n = CFArrayGetCount(ms);
NSSize ns;
for(int i = 0; i < n; ++i){
    CGDisplayModeRef m = (CGDisplayModeRef)CFArrayGetValueAtIndex(ms, i);
    if(CGDisplayModeGetIOFlags(m) & kDisplayModeNativeFlag){
        ns.width = CGDisplayModeGetPixelWidth(m);
        ns.height = CGDisplayModeGetPixelHeight(m);
        break;
    }
}
CFRelease(ms);

I would go a different route. Instead of finding out the screen dimensions, I would fetch the device model number that the program was being run on and then compare the model number to the dimensions of its screen. This may be tedious to program the model number and corresponding screen size but thats the only way I can think of. Hope this helps.

If using NSScreen is an option, you could do something like this in OSX 10.7:

NSRect framePixels = [screen convertRectToBacking:[screen frame]];

where framePixels.size is your display's pixel resolution and screen is a pointer to NSScreen . For example, this code would print the pixel resolution of all active displays to console:

for (NSScreen* screen in [NSScreen screens])
{
    NSRect framePixels = [screen convertRectToBacking:[screen frame]];
    NSLog(@"framePixels: (%f, %f)", framePixels.size.width, framePixels.size.height);
}

system_profiler SPDisplaysDataType | grep Resolution:

On a two display machine, I get this output:

      Resolution: 2880 x 1800 Retina
      Resolution: 2560 x 1440 (QHD/WQHD - Wide Quad High Definition)

I heard about this from a similar question: How to get the physical display resolution on MacOS?

Modified version of Ken/jxy solution above, by default uses screen frame*backingScaleFactor if no native resolution is found:

static float getScreenScaleFactor() {
NSRect screenFrame = [[NSScreen mainScreen] frame];

// this may well be larger than actual pixel dimensions of screen, as Mac will report the backing scale factor of the render buffer, not the screen, stupidly
float bestWidth = screenFrame.size.width * [[NSScreen mainScreen] backingScaleFactor];

// if there's a native resolution found in this method, that's more accurate than above
CFArrayRef myModes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL);
for (int i = 0; i < CFArrayGetCount(myModes); i++) {
    CGDisplayModeRef myMode = (CGDisplayModeRef) CFArrayGetValueAtIndex(myModes, i);
    if (CGDisplayModeGetIOFlags(myMode) & kDisplayModeNativeFlag) {
        bestWidth = CGDisplayModeGetPixelWidth(myMode);
        //printf("found native resolution: %i %i\n", (int)CGDisplayModeGetPixelWidth(myMode), (int)CGDisplayModeGetPixelHeight(myMode));
        break;
        }
    }

return bestWidth/screenFrame.size.width;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM