如何读取OSX的物理屏幕尺寸? [英] How to read the physical screen size of OSX?
问题描述
我想知道Mac OSX下的物理屏幕尺寸.但是NSDeviceResolution
总是报告错误的值(72),因此分辨率/dpi的计算结果是错误的.
I would like to know the physical screen size under Mac OSX. But NSDeviceResolution
is always reporting wrong value (72), so the calculation result of resolution / dpi is wrong.
在关于本机"内,有一个Mac型号的琴弦,我的是"15英寸,2011年初".因此,我想知道是否应该有一种方法(可能在obj-c中)读取该字符串,然后将其用作物理屏幕尺寸.
Inside "About This Mac", there is a Mac model string there, mine is "15-inch, Early 2011". So I'm wondering should there be a way (in obj-c probably), to read that string and then I can use that as the physical screen size.
感谢您的帮助.
推荐答案
您可以使用 CGDisplayScreenSize
来获取屏幕的物理尺寸(以毫米为单位).假设您已经知道分辨率,则可以从中计算DPI.
You can use CGDisplayScreenSize
to get the physical size of a screen in millimetres. From that you can compute the DPI given that you already know the resolution.
例如.
NSScreen *screen = [NSScreen mainScreen];
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(
[[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
NSLog(@"DPI is %0.2f",
(displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
// there being 25.4 mm in an inch
该@"NSScreenNumber"
内容看起来很狡猾,但它是明确记录的手段从NSScreen
获得CGDirectDisplayID
的a>.
That @"NSScreenNumber"
thing looks dodgy but is the explicit documented means of obtaining a CGDirectDisplayID
from an NSScreen
.
这篇关于如何读取OSX的物理屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!