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

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

Given a CGDirectDisplayID returned from

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

for the built-in s

5条回答
  •  猫巷女王i
    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);
    

提交回复
热议问题