Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

core graphics - How to get the physical display resolution on MacOS?

I'm looking to find the value that "About this Mac" shows (2560 x 1600 on my 13" MBP). I have tried CGDisplayBounds and NSScreen.main, both do not return those values but instead return what is used internal for rendering / measuring.

as by Kens suggestion:

let modes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), [kCGDisplayShowDuplicateLowResolutionModes: kCFBooleanTrue] as CFDictionary) as! [CGDisplayMode]

for mode in modes {
    let flags = String(format:"%02X", mode.ioFlags)
    print("(mode.pixelWidth)x(mode.pixelHeight) (mode.width)x(mode.height) 0x(flags)")
}

Output is:

2560x1600 2560x1600 0x2000003 <- This would be the correct one
...
2880x1800 2880x1800 0x03 <- This one is the biggest 1x mode
...

So using the biggest 1x would get the wrong result. I added the ioFlags to the output. I belive this might the missing link;-)

Thank you Ken!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the best approach is to enumerate all of the display modes (including the 1x modes) and find a) one whose ioFlags includes kDisplayModeNativeFlag, or, if none has that flag, b) 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...