How to get all possible screen resolutions in cocoa?

做~自己de王妃 提交于 2019-12-13 03:40:44

问题


I want to fetch all the supported resolutions for mac screen. I'm using the below code for achieving all supported resolutions:

CFArrayRef modeList;
modeList=CGDisplayCopyAllDisplayModes(displays[i], NULL);

By using the above code, I'm only getting the resolutions as shown in below image:

I have installed one app which shows supported resolutions for my mac screen. It shows the resolutions as shown into below image. I also want to get the higher resolutions as they are showing.

I have referred the below link: CGDisplayCopyAllDisplayModes leaves out one valid mode

But I don't know how to get other supported resolutions using kCGDisplayShowDuplicateLowResolutionModes.


回答1:


The accepted answer at the link you provide does present the answer you need, though it's using an undocumented options flag which is unfortunate. You have to pass in the options dictionary to your call to CGDisplayCopyAllDisplayModes as it indicates:

CGDirectDisplayID mainDisplayID = CGMainDisplayID();
CFStringRef keys[1] = { kCGDisplayShowDuplicateLowResolutionModes };
CFBooleanRef values[1] = { kCFBooleanTrue };

CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, (const void**) keys, (const void**) values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );

CFArrayRef modesArray = CGDisplayCopyAllDisplayModes( mainDisplayID, options );

and then look through the returned display modes seems to work for me. Note that you'll probably want to check the boolean returned by CGDisplayModeIsUsableForDesktopGUI(…) to filter out those that are invalid (depending on your use case).

Obviously you'll be passing in each of the different display ids in turn and get output more like this:

Display 3: external, main
points: (2560 x 1440), pixels: (2560, 1440)
points: (1280 x 720), pixels: (1280, 720)
points: (800 x 600), pixels: (800, 600)
points: (1024 x 768), pixels: (1024, 768)
points: (1280 x 960), pixels: (1280, 960)
points: (1344 x 1008), pixels: (1344, 1008)
points: (1344 x 756), pixels: (1344, 756)
points: (1600 x 1200), pixels: (1600, 1200)
points: (1600 x 900), pixels: (1600, 900)
points: (2048 x 1152), pixels: (2048, 1152)

Display 2: builtin,
points: (2880 x 1800), pixels: (2880, 1800)
points: (1440 x 900), pixels: (1440, 900)
points: (3360 x 2100), pixels: (3360, 2100)
points: (2560 x 1600), pixels: (2560, 1600)
points: (2048 x 1280), pixels: (2048, 1280)
points: (1650 x 1050), pixels: (1650, 1050)
points: (1280 x 800), pixels: (1280, 800)
points: (1152 x 720), pixels: (1152, 720)
points: (1024 x 768), pixels: (1024, 768)
points: (800 x 600), pixels: (800, 600)

Display 5: external,
points: (1920 x 1200), pixels: (1920, 1200)
points: (960 x 600), pixels: (960, 600)
points: (800 x 600), pixels: (800, 600)
points: (1024 x 768), pixels: (1024, 768)
points: (1024 x 640), pixels: (1024, 640)
points: (1280 x 960), pixels: (1280, 960)
points: (1280 x 800), pixels: (1280, 800)
points: (1344 x 1008), pixels: (1344, 1008)
points: (1344 x 840), pixels: (1344, 840)
points: (1600 x 1200), pixels: (1600, 1200)
points: (1600 x 1000), pixels: (1600, 1000)

Also note that these numbers don't line up with what's showing for MBP, for example. "looks like 1920 x 1200" which doesn't match any of the listed resolutions.


Another dev was having trouble getting this to work, so I pushed my quick hacked together test project that works up to github so others can see the entire working project. Hopefully that'll help someone (DO clean up that code before you use it in any kind of product though - it's just a quickest possible hacked together test code).



来源:https://stackoverflow.com/questions/53064744/how-to-get-all-possible-screen-resolutions-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!