macos Mac OS X:对视网膜显示进行运行时检查的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11067066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:15:57  来源:igfitidea点击:

Mac OS X: Best way to do runtime check for retina display?

objective-cmacoscocoaosx-lionretina-display

提问by Todd Ditchendorf

Given a Cocoa application which runs on Mac OS X 10.7 and later:

给定在 Mac OS X 10.7 及更高版本上运行的 Cocoa 应用程序:

What is the best way to check, at runtime, if your app is currently running on a Mac with at least one retina display attached?

在运行时检查您的应用程序当前是否在至少连接了一个视网膜显示器的 Mac 上运行的最佳方法是什么?

If checking for this sort of thing is just really wrong-headed, I fully welcome a well-reasoned explanation of why (and I will up-vote such answers if they are good).

如果检查这类事情真的是错误的,我完全欢迎对原因的合理解释(如果这些答案很好,我会投票赞成)。

But I'd still like to know :).

但我还是想知道:)。

It seems likely you could just do a check specifically for the new Mac Book Pro "Retina" hardware (the only Mac which currently has a retina display), but I'd really prefer a more general/generic/future-proof way to check than this.

您似乎可以专门针对新的 Mac Book Pro“Retina”硬件(目前唯一具有视网膜显示屏的 Mac)进行检查,但我真的更喜欢使用更通用/通用/面向未来的方法来检查比这个。

Ideally, I'd like to know how to detect the retina display, not the specific Mac model which currently happens to ship with a retina display.

理想情况下,我想知道如何检测视网膜显示器,而不是当前碰巧随附视网膜显示器的特定 Mac 型号。

回答by tjw

If you really do need to do this, take a look at -[NSScreen backingScaleFactor]. But, this does seem wrong-headed, at least without knowing more about why you want to know.

如果您确实需要这样做,请查看-[NSScreen backingScaleFactor]. 但是,这似乎是错误的,至少在不了解您想知道的更多信息的情况下。

While there is currently only one Mac with a Retina display, there may eventually be standalone displays that support Retina (and can be attached/detached at runtime) and you may be able to configure the built-in Retina display in 1x mode. Thus the answer to the question "is there a Retina display attached" can change at any time.

虽然目前只有一台配备 Retina 显示器的 Mac,但最终可能会有支持 Retina 的独立显示器(并且可以在运行时连接/分离),并且您可以将内置 Retina 显示器配置为 1x 模式。因此,“是否连接了 Retina 显示器”这个问题的答案随时可能发生变化。

Rather, you may want to know if your content should be drawn with a given scale by using the -convert*ToBacking:methods or -[NSWindow backingScaleFactor]. For a ton more detail, watch the WWDC 2012 session video "Advanced Tips and Tricks for High Resolution on OS X" (when posted, hopefully within the next few weeks).

相反,您可能想知道您的内容是否应该通过使用-convert*ToBacking:或 方法以给定的比例绘制-[NSWindow backingScaleFactor]。有关更多详细信息,请观看 WWDC 2012 会议视频“在 OS X 上实现高分辨率的高级技巧和窍门”(发布后,希望在接下来的几周内)。

回答by nuclearnova

I just ran into an instance where I needed to detect if any screen was high resolution, this worked

我刚遇到一个实例,我需要检测是否有任何屏幕是高分辨率的,这行得通

float displayScale = 1;
    if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
        NSArray *screens = [NSScreen screens];
        NSUInteger screenCount = screens.count
        for (int i = 0; i < screenCount; i++) {
            float s = [screens[i] backingScaleFactor];
            if (s > displayScale)
                displayScale = s;
        }
    }

回答by Dave DeLong

Well, on iOS you use the UIScreen.scaleproperty. If it returns 2.0, then you're on a device with a high resolution display. Otherwise you're on a low-resolution device.

好吧,在 iOS 上,您使用该UIScreen.scale属性。如果它返回2.0,那么您使用的是具有高分辨率显示器的设备。否则,您将使用低分辨率设备。

So I'd imagine that on Mac OS you could use either -[NSScreen backingScaleFactor]or -[NSWindow backingScaleFactor].

所以我想在 Mac OS 上你可以使用-[NSScreen backingScaleFactor]-[NSWindow backingScaleFactor].