CGDisplayIOServicePort在OS X> = 10.9中已弃用,该如何替换? [英] CGDisplayIOServicePort is deprecated in OS X >= 10.9, how to replace?

查看:103
本文介绍了CGDisplayIOServicePort在OS X> = 10.9中已弃用,该如何替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小型应用程序,以便快速更改多台显示器的屏幕分辨率.我想将产品名称显示为监视器的标题,使用此代码可以很容易地找到它:

I did small app to allow quickly change screen resolutions on multiple monitors. I want to show product name as title of the monitor, and it's very simple to find using this code:

NSDictionary *deviceInfo = (__bridge NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(dispID), kIODisplayOnlyPreferredName);

NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];

if([localizedNames count] > 0) {
    _title = [localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]];
} else {
    _title = @"Unknown display";
}

但是在OS X> = 10.9中不推荐使用 CGDisplayIOServicePort ,Apple的文档说没有替代品.不使用此方法如何查找服务端口或产品名称?

But CGDisplayIOServicePort is deprecated in OS X >= 10.9 and Apple's documentation says there is no replacement. How to find service port or product name without using this method?

我尝试遍历IO注册表,并尝试使用 IOServiceGetMatchingServices 方法来查找显示服务,但是我对IO注册表不是很熟悉,因此找不到解决方案.

I tried to iterate through IO-registry and tried to use IOServiceGetMatchingServices method to find display services but I'm not very familiar with IO-registry so I couldn't find solution.

感谢帮助!

推荐答案

@Eun的帖子似乎丢失了一些信息,以结束本讨论.稍作搜索,我发现IOServicePortFromCGDisplayID不是Apple提供的API.而是在这里找到了一段开放源代码: https://github.com/glfw/glfw/blob/e0a6772e5e4c672179/cocoa_monitor.m

It looks like @Eun's post missed a piece of information to close this discussion. With a little search, I found that IOServicePortFromCGDisplayID is not an API which Apple provides. Rather, it's a piece of open source code found here: https://github.com/glfw/glfw/blob/e0a6772e5e4c672179fc69a90bcda3369792ed1f/src/cocoa_monitor.m

我复制了IOServicePortFromCGDisplayID,还从中复制了"getDisplayName".我需要进行两项调整才能使其在OS X 10.10上运行.

I copied IOServicePortFromCGDisplayID and also 'getDisplayName' from it. I needed two tweaks to make it work on OS X 10.10.

  1. 删除代码以处理IOServicePortFromCGDisplayID中的序列号.(CFDictionaryGetValue为kDisplaySerialNumber为我返回NULL.)
  2. 删除特定项目getDisplayName中的错误处理代码.

如果您需要更多信息

  • 问题的问题跟踪者:github.com/glfw/glfw/issues/165
  • 提交解决方案:github.com/glfw/glfw/commit/e0a6772e5e4c672179fc69a90bcda3369792ed1f

我要感谢在那提交了代码的Matthew Henry.

I would thank Matthew Henry who submitted the code there.

这篇关于CGDisplayIOServicePort在OS X> = 10.9中已弃用,该如何替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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