如何在 MacOS 上获取物理显示分辨率? [英] How to get the physical display resolution on MacOS?

查看:32
本文介绍了如何在 MacOS 上获取物理显示分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于这台 Mac"显示的值(在我的 13 英寸 MBP 上为 2560 x 1600).我尝试了 CGDisplayBounds 和 NSScreen.main,两者都不返回这些值,而是返回使用的值内部用于渲染/测量.

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.

根据肯斯的建议:

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 添加到输出中.我相信这可能是缺少的链接;-)

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

谢谢肯!

推荐答案

我认为最好的方法是枚举所有显示模式(包括 1x 模式)并找到一个具有 ioFlags包括 kDisplayModeNativeFlag,或者,如果没有该标志,b) 最大 1x 模式的尺寸.

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.

您将使用 CGDisplayCopyAllDisplayModes() 并传递一个字典,其中键 kCGDisplayShowDuplicateLowResolutionModes 映射到 kCFBooleanTrue 作为 options> 获取所有模式.您可以测试 CGDisplayModeGetPixelWidth() 是否等于 CGDisplayModeGetWidth() 以确定哪些是 1x.

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.

这篇关于如何在 MacOS 上获取物理显示分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆