使用CGDisplayStream捕获屏幕 [英] Capture screen with CGDisplayStream

查看:422
本文介绍了使用CGDisplayStream捕获屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现一项功能,该功能将记录屏幕.我有一些示例代码和WWDC 2012视频中找到的代码.

I'm trying to implement a feature in my app that will record the screen. I have bits of code that I have found in some sample code and a WWDC 2012 video.

到目前为止,我有这个.

So far I have this.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    // Get a list of displays.  Copied from working apple source code.
    [self getDisplayList];

    DisplayRegistrationCallBackSuccessful = NO;

    // Register the event for modifying displays.
    CGError err =  CGDisplayRegisterReconfigurationCallback(DisplayRegisterReconfigurationCallback, NULL);
    if (err == kCGErrorSuccess) {
        DisplayRegistrationCallBackSuccessful = YES;
    }

    // Insert code here to initialize your application

    const void *keys[1] = { kCGDisplayStreamSourceRect };
    const void *values[1] = { CGRectCreateDictionaryRepresentation(CGRectMake(0, 0, 100, 100)) };
    CFDictionaryRef properties = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

    stream = CGDisplayStreamCreate(displays[0], 100, 100, '420f', properties,
                                                  ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
                                                      NSLog(@"Next Frame"); // This line is never called.
                                                  });

    runLoop = CGDisplayStreamGetRunLoopSource(stream);

    CGError err = CGDisplayStreamStart(stream); 
    if (err == CGDisplayNoErr) {
        NSLog(@"Works");
    } else {
        NSLog(@"Error: %d", err);
    }
}

我遇到的问题是未调用DisplayStream的回调块.我没有收到任何错误或警告.有什么我想念的东西或做错了吗?

The problem I am encountering is that the callback block for the DisplayStream is not being called. I am not getting any errors or warnings. Is there something I'm missing or that I've done wrong?

推荐答案

我通过使用CGDisplayStreamCreateWithDispatchQueue并将dispatch_queue_create("queue for dispatch", NULL);作为队列来解决了这个问题.

I solved the problem by using CGDisplayStreamCreateWithDispatchQueue and passing dispatch_queue_create("queue for dispatch", NULL); as the queue.

这篇关于使用CGDisplayStream捕获屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆