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

南笙酒味 提交于 2019-11-28 04:58:36

问题


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?


回答1:


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);



回答2:


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.




回答3:


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.




回答4:


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);
}



回答5:


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?



来源:https://stackoverflow.com/questions/13859109/how-to-programmatically-determine-native-pixel-resolution-of-retina-macbook-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!