gpt4 book ai didi

objective-c - 如何在屏幕截图上忽略我的应用程序窗口? ( swift 2.0)

转载 作者:搜寻专家 更新时间:2023-11-01 06:39:45 24 4
gpt4 key购买 nike

我想获取屏幕图像,忽略我应用程序的窗口。

在 Objective-C 中找到一个代码示例并尝试将其转换为 Swift。

Objective-C 删减:

// Get onscreen windows
CGWindowID windowIDToExcude = (CGWindowID)[myNSWindow windowNumber];
CFArrayRef onScreenWindows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
CFMutableArrayRef finalList = CFArrayCreateMutableCopy(NULL, 0, onScreenWindows);
for (long i = CFArrayGetCount(finalList) - 1; i >= 0; i--) {
CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(finalList, i);
if (window == windowIDToExcude)
CFArrayRemoveValueAtIndex(finalList, i);
}
// Get the composite image
CGImageRef ref = CGWindowListCreateImageFromArray(myRectToGrab, finalList, kCGWindowListOptionAll);

我在 Swift 中的版本(到目前为止我设法做到了):

// Get onscreen windows
let windowIDToExcude = myNSWindow.windowNumber!
let onScreenWindows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID)
let finalList = CFArrayCreateMutableCopy(nil, 0, onScreenWindows)

for var i = CFArrayGetCount(finalList) - 1; i >= 0; i-=1 {
var window: CGWindowID = (uintptr_t(CFArrayGetValueAtIndex(finalList, i)) as! CGWindowID)
if window == windowIDToExcude {
CFArrayRemoveValueAtIndex(finalList, i)
}
}
// Get the composite image
var ref = CGWindowListCreateImageFromArray(myRectToGrab, finalList, kCGWindowListOptionAll)

但它在 swift 2.0 中不起作用,我不知道为什么。

特别是这一行无法编译:

CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID)

显然,不再有CGWindowListCreatekCGWindowListOptionOnScreenOnlykCGNullWindowID 这样的东西了。

最佳答案

你能试试这个吗:

let imageRef = CGWindowListCreateImage(self.view.frame, CGWindowListOption.OptionOnScreenBelowWindow, CGWindowID(self.view.window!.windowNumber), CGWindowImageOption.Default)

let image = NSImage(CGImage: imageRef!, size: self.view.frame.size)
self.imageView.image = image

这对我有用。

关于objective-c - 如何在屏幕截图上忽略我的应用程序窗口? ( swift 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com