Cocoa:截取桌面壁纸(没有图标和窗口)

发布于 2024-12-14 19:13:36 字数 284 浏览 3 评论 0原文

是否可以在没有桌面项目和任何可能打开的窗口(即仅壁纸)的情况下捕获 Mac OS X 桌面?

我尝试过 CGWindowListCreateImage、CGWindowListCreateImageFromArrayCGDisplayCreateImage,但没有运气。

本质上,我试图在不使用 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三五鸿雁 2024-12-21 19:13:36

您需要小心测试这是否仍然正确,但桌面窗口位于 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 to CGWindowListCreateImage 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 level kCGMinimumWindowLevel + 19.

It gets kind of tricky when there are multiple screens, but hopefully this information is enough for you to do what you need.

绳情 2024-12-21 19:13:36

我知道这是一个超级老的问题,托尼·阿诺德的问题是正确的,我用什么来构建自己的“抓取桌面”代码。

我有一些示例代码,展示了如何完成所有这些事情(在几乎没有记录的 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 )

皇甫轩 2024-12-21 19:13:36

斯威夫特版本:

extension NSImage {

    static func desktopPicture() -> NSImage {

        let windows = CGWindowListCopyWindowInfo(
            CGWindowListOption.OptionOnScreenOnly,
            CGWindowID(0))! as NSArray

        var index = 0
        for var i = 0; i < windows.count; i++  {
            let window = windows[i]

            // we need windows owned by Dock
            let owner = window["kCGWindowOwnerName"] as! String
            if owner != "Dock" {
                continue
            }

            // we need windows named like "Desktop Picture %"
            let name = window["kCGWindowName"] as! String
            if !name.hasPrefix("Desktop Picture") {
                continue
            }

            // wee need the one which belongs to the current screen
            let bounds = window["kCGWindowBounds"] as! NSDictionary
            let x = bounds["X"] as! CGFloat
            if x == NSScreen.mainScreen()!.frame.origin.x {
                index = window["kCGWindowNumber"] as! Int
                break
            }
        }

        let cgImage = CGWindowListCreateImage(
            CGRectZero,
            CGWindowListOption(arrayLiteral: CGWindowListOption.OptionIncludingWindow),
            CGWindowID(index),
            CGWindowImageOption.Default)!

        let image = NSImage(CGImage: cgImage, size: NSScreen.mainScreen()!.frame.size)
        return image
    }
}

Swift version:

extension NSImage {

    static func desktopPicture() -> NSImage {

        let windows = CGWindowListCopyWindowInfo(
            CGWindowListOption.OptionOnScreenOnly,
            CGWindowID(0))! as NSArray

        var index = 0
        for var i = 0; i < windows.count; i++  {
            let window = windows[i]

            // we need windows owned by Dock
            let owner = window["kCGWindowOwnerName"] as! String
            if owner != "Dock" {
                continue
            }

            // we need windows named like "Desktop Picture %"
            let name = window["kCGWindowName"] as! String
            if !name.hasPrefix("Desktop Picture") {
                continue
            }

            // wee need the one which belongs to the current screen
            let bounds = window["kCGWindowBounds"] as! NSDictionary
            let x = bounds["X"] as! CGFloat
            if x == NSScreen.mainScreen()!.frame.origin.x {
                index = window["kCGWindowNumber"] as! Int
                break
            }
        }

        let cgImage = CGWindowListCreateImage(
            CGRectZero,
            CGWindowListOption(arrayLiteral: CGWindowListOption.OptionIncludingWindow),
            CGWindowID(index),
            CGWindowImageOption.Default)!

        let image = NSImage(CGImage: cgImage, size: NSScreen.mainScreen()!.frame.size)
        return image
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文