如何在 MacOS 上获取物理显示分辨率?
How to get the physical display resolution on MacOS?
我正在寻找 "About this Mac" 显示的值(在我的 13" MBP 上为 2560 x 1600)。我尝试了 CGDisplayBounds 和 NSScreen.main,两者都不 return这些值而是 return 内部用于渲染/测量的值。
根据 Kens 的建议:
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)")
}
输出为:
2560x1600 2560x1600 0x2000003 <- This would be the correct one
...
2880x1800 2880x1800 0x03 <- This one is the biggest 1x mode
...
所以使用最大的 1x 会得到错误的结果。我将 ioFlags 添加到输出中。我相信这可能是缺失的 link;-)
谢谢肯!
我认为最好的方法是枚举所有显示模式(包括 1x 模式)并找到 a) ioFlags
包括 kDisplayModeNativeFlag
的模式,或者,如果 none有那个标志,b) 最大的 1x 模式的尺寸。
您将使用 CGDisplayCopyAllDisplayModes()
并传递一个字典,其中键 kCGDisplayShowDuplicateLowResolutionModes
映射到 kCFBooleanTrue
作为 options
以获取所有模式。您可以测试 CGDisplayModeGetPixelWidth()
等于 CGDisplayModeGetWidth()
以确定哪些是 1x。
NSScreen
有 backingScaleFactor
属性 检查规模
我正在寻找 "About this Mac" 显示的值(在我的 13" MBP 上为 2560 x 1600)。我尝试了 CGDisplayBounds 和 NSScreen.main,两者都不 return这些值而是 return 内部用于渲染/测量的值。
根据 Kens 的建议:
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)")
}
输出为:
2560x1600 2560x1600 0x2000003 <- This would be the correct one
...
2880x1800 2880x1800 0x03 <- This one is the biggest 1x mode
...
所以使用最大的 1x 会得到错误的结果。我将 ioFlags 添加到输出中。我相信这可能是缺失的 link;-)
谢谢肯!
我认为最好的方法是枚举所有显示模式(包括 1x 模式)并找到 a) ioFlags
包括 kDisplayModeNativeFlag
的模式,或者,如果 none有那个标志,b) 最大的 1x 模式的尺寸。
您将使用 CGDisplayCopyAllDisplayModes()
并传递一个字典,其中键 kCGDisplayShowDuplicateLowResolutionModes
映射到 kCFBooleanTrue
作为 options
以获取所有模式。您可以测试 CGDisplayModeGetPixelWidth()
等于 CGDisplayModeGetWidth()
以确定哪些是 1x。
NSScreen
有 backingScaleFactor
属性 检查规模