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

前端 未结 5 1493
礼貌的吻别
礼貌的吻别 2020-12-18 04:51

Given a CGDirectDisplayID returned from

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

for the built-in s

相关标签:
5条回答
  • 2020-12-18 05:24

    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.

    0 讨论(0)
  • 2020-12-18 05:30

    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.

    0 讨论(0)
  • 2020-12-18 05:40

    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);
    
    0 讨论(0)
  • 2020-12-18 05:40

    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?

    0 讨论(0)
  • 2020-12-18 05:41

    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);
    }
    
    0 讨论(0)
提交回复
热议问题