Cocoa:截取桌面壁纸(没有图标和窗口)
是否可以在没有桌面项目和任何可能打开的窗口(即仅壁纸)的情况下捕获 Mac OS X 桌面?
我尝试过 CGWindowListCreateImage、CGWindowListCreateImageFromArray
和 CGDisplayCreateImage
,但没有运气。
本质上,我试图在不使用 [NSWorkspacedesktopImageURLForScreen:]
的情况下捕获桌面壁纸(它是一个沙盒应用程序,无法访问文件系统)。
Is is possible to capture the Mac OS X desktop without desktop items and any windows that may be open (i.e. just the wallpaper)?
I've experimented with CGWindowListCreateImage
, CGWindowListCreateImageFromArray
, and CGDisplayCreateImage
, but no luck.
Essentially I'm trying to capture the desktop wallpaper without using [NSWorkspace desktopImageURLForScreen:]
(it's a sandboxed app without access to the file system).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要小心测试这是否仍然正确,但桌面窗口位于 Finder 下方(它由 Dock 绘制)。将
kCGWindowListOptionOnScreenBelowWindow
CGWindowListOption
传递给CGWindowListCreateImage
应该可以得到你想要的东西(除非其他东西在该级别以下绘制)。否则,您需要使用
CGWindowListCreate
并迭代响应,排除窗口级别kCGMinimumWindowLevel + 19
上的停靠栏未绘制的任何内容。当有多个屏幕时,这会变得有点棘手,但希望这些信息足以让您完成您需要的操作。
You'll need to be careful to test that this is still correct, but the desktop window sits below the Finder (it's drawn by the Dock). Passing the
kCGWindowListOptionOnScreenBelowWindow
CGWindowListOption
toCGWindowListCreateImage
should get you what you want (unless something else is drawing below that level).Otherwise, you'll need to use
CGWindowListCreate
and iterate through the response excluding anything that isn't drawn by the dock at the window levelkCGMinimumWindowLevel + 19
.It gets kind of tricky when there are multiple screens, but hopefully this information is enough for you to do what you need.
我知道这是一个超级老的问题,托尼·阿诺德的问题是正确的,我用什么来构建自己的“抓取桌面”代码。
我有一些示例代码,展示了如何完成所有这些事情(在几乎没有记录的 Cocoa 部分中行走是一件很棒的事情......)
我已将该示例代码放在 bitbucket 存储库中。具体来说拍照的代码示例。 (我的 学习 Cocoa 存储库中有更有趣的 Cocoa 代码,示例代码来自 )
I know this is a super old question, and Tony Arnold's question is right, and what I used to build my own "grab the desktop" code.
I have some example code that shows how to do all these things (it's a wonderful thing walking in parts of Cocoa that are barely documented... )
I've put that sample code up in a bitbucket repository. Specifically the code sample to take a picture. (There's more interesting Cocoa code in my learning Cocoa repository, where that sample code is from )
斯威夫特版本:
Swift version: