上面修改版的 Ken/jxy 解决方案,如果没有找到原生分辨率,默认使用 screen frame*backingScaleFactor:
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;
}