使用 Objective C for Mac 进行屏幕截图

发布于 2024-08-09 17:41:25 字数 94 浏览 6 评论 0原文

有谁知道如何在 macOS 中使用 Objective C 捕获屏幕? 更具体地说,如何捕获活动/聚焦的应用程序屏幕,然后在指定路径中创建图像。
非常感谢任何帮助。

Does anyone has any idea how can I capture screen using objective c in mac os?
to be more specific, how can I capture the active / focused application screen then create an image into a specified path.
Any help is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

依 靠 2024-08-16 17:41:25

@丹尼尔,
你不需要理解和实现整个“Grab之子”。您只需要下面的代码。

以下函数将为您提供屏幕截图

// This just invokes the API as you would if you wanted to grab a screen shot. The equivalent using the UI would be to
// enable all windows, turn off "Fit Image Tightly", and then select all windows in the list.
CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);

使用以下代码将其转换为 NSImage

NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
bitmapRep = nil;

@Daniel,
You don't need to understand and implement the whole "Son of Grab". You just need the code below.

The following function will give you the screenshot

// This just invokes the API as you would if you wanted to grab a screen shot. The equivalent using the UI would be to
// enable all windows, turn off "Fit Image Tightly", and then select all windows in the list.
CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);

Use the following code to convert it to a NSImage

NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
bitmapRep = nil;
戈亓 2024-08-16 17:41:25

您查看过Apple的 “Son of Grab” 用于使用 CGWindow api 捕获窗口图像?

Have you checked out Apple's “Son of Grab” for capturing images of windows with the CGWindow api?

红玫瑰 2024-08-16 17:41:25

您还可以检查Apple的OpenGLScreenSnapshot

You can also check Apple's OpenGLScreenSnapshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文