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

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

问题描述

我正在寻找关于本机"显示的值(在我的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.

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

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

谢谢Ken!

推荐答案

我认为最好的方法是枚举所有显示模式(包括1x模式)并找到a)一种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天全站免登陆