objective-c - 仅在 mac 中以编程方式截取应用程序窗口的屏幕截图

标签 objective-c macos cocoa screenshot

我必须为我的 OSX 应用程序添加截屏功能,但 Apple 演示只给我整个屏幕图像,我只想要我的应用程序窗口图像。

我的代码是:

NSInteger displaysIndex = 0;
if(displays != nil)
{
    free(displays);
}


CGError             err = CGDisplayNoErr;
CGDisplayCount      dspCount = 0;

/* How many active displays do we have? */
err = CGGetActiveDisplayList(0, NULL, &dspCount);

/* If we are getting an error here then their won't be much to display. */
if(err != CGDisplayNoErr)
{
    return;
}
/* Allocate enough memory to hold all the display IDs we have. */
displays = calloc((size_t)dspCount, sizeof(CGDirectDisplayID));

// Get the list of active displays
err = CGGetActiveDisplayList(dspCount,
                             displays,
                             &dspCount);

/* Make a snapshot image of the current display. */
CGImageRef image = CGDisplayCreateImage(displays[displaysIndex]);

我应该更改代码中的哪些内容才能只获取应用程序的窗口?

最佳答案

简单。

NSImage *captureImage  = [[NSImage alloc] initWithData:[self.view dataWithPDFInsideRect:[self.view bounds]]];

请检查并告诉我。这是捕获的当前事件窗口。

关于objective-c - 仅在 mac 中以编程方式截取应用程序窗口的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15446319/

相关文章:

ios - 从字符串格式化日期?

ios - 当应用程序进入前台时检测远程通知

如果鼠标移动太快,SwiftUI onHover 不会注册鼠标离开元素

macos - 在 WebView 中捕获 mailto 链接

cocoa - 苹果/ cocoa : Embed a terminal window in my app

objective-c - 更改预定义颜色的不透明度?

ios - 如何在 map View 中拖动注释图钉?

objective-c - iOS:什么是 Objective-C 中的事件监听器?

macos - Mac OSX - 允许用户通过 GUI 或提示符在 shell 脚本中输入

cocoa - 更改 NSTextField 中的行高(前导)