【问题标题】:screen capture using objective c for mac使用objective c for mac截屏
【发布时间】:2010-12-13 17:44:23
【问题描述】:

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

【问题讨论】:

    标签: objective-c screen capture


    【解决方案1】:

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

    【讨论】:

    • 是的,我已经看到了,我对目标 c 比较陌生,Grab 之子对我来说有点困难。我的要求很简单,创建一个在后台运行的应用程序,在满足某些条件时,只需捕获当前焦点窗口并在指定路径上创建图像
    • 您好,“Son of Grab”的链接已失效...您能告诉我在哪里可以找到它吗?
    【解决方案2】:

    @丹尼尔, 你不需要理解和实现整个“Son of 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;
    

    【讨论】:

    • 记得打电话给CFRelease(screenShot);,以免泄露。
    • 辛苦学习@Aaron
    【解决方案3】:

    你也可以查看苹果的 OpenGLScreenSnapshot

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 2018-08-08
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2011-05-30
      相关资源
      最近更新 更多