2

我一直在试图弄清楚如何在目标 C 中创建一个 OS X 例程:

  • 在后台运行(单独的线程)和

  • 重复(定时或满足某些条件时)捕获指定窗口/屏幕的几个定义子集(即没有用户交互)和

  • 将图像保存到指定路径。

类似于“cmd-shift-4 + mouseDown-drag 的预编程循环”的输出。

Mac Developer Library 中的 SonofGrab 和 ScreenSnapshot 似乎都没有表明如何做到这一点,但我怀疑 CGImageRef 是一种/(?)方法。

  1. 有谁知道该怎么做?

  2. 除了 CGImageRef 还有其他可能的方法来解决这个问题吗?例如,它可以通过命令行工具和 NSTask 来完成吗?

  3. 如果是这样,就速度和复杂性而言,不同方法的优点/缺点是什么?

4

1 回答 1

2

假设您具有执行屏幕抓取所需的逻辑(您可以在 SonOfGrab 中看到),您只需使用调度队列或计时器来完成这项工作。

void MyTakeScreengrab();

dispatch_queue_t  queue    = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,  0);

dispatch_source_t timerSrc = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,queue);

dispatch_source_set_timer(timerSrc, 
      dispatch_time(DISPATCH_TIME_NOW,0) /* start on */,
      SECONDS_PER_GRAB * NSEC_PER_SEC /* interval */, 
      NSEC_PER_SEC /* leeway */);

dispatch_source_set_event_handler_f(timerSrc, MyTakeScreengrab);

dispatch_resume(timerSrc);

您也可以使用 NSTimer。

-(void)setup {
    _timer = [[NSTimer scheduledTimerWithTimeInterval: (NSTimerInterval)SECONDS_PER_GRAB 
            target: self 
            selector: @selector(takeScreegrabOnBGThread:) 
            userInfo: @{ @"folderPath" : MyFolderPath(), 
                         @"imageSettings" : MYCGImageSettings() }  
            repeats: YES];
    [_timer fire];
}

-(void)takeScreengrabOnBGThread:(id)userInfo {
    [self performSelectorInBackground:@selector(takeScreengrabBlocking:) 
            withObject:userInfo];
}

-(void)takeScreengrabBlocking:(id)userInfo {

    MyTakeScreengab(userInfo);
}

要使用终端执行此操作,您要查找的命令是screencapture(1).

于 2013-02-08T00:31:12.980 回答