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

时间:2013-03-16 06:30:35

标签: 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]);

我应该在代码中更改哪些内容,以便只显示应用程序的窗口?

2 个答案:

答案 0 :(得分:7)

简单。

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

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

答案 1 :(得分:7)

要实际拍摄窗口的屏幕截图,包括其框架和阴影,请获取窗口windowNumber并将其传递给the CGWindowListCreateImage function