2012-12-24 123 views
5

我试图在我的应用程序中实现一项功能,该功能将记录屏幕。我在一些示例代码和WWDC 2012视频中找到了一些代码。使用CGDisplayStream捕获屏幕

到目前为止,我有这个。

- (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的回调块。我没有收到任何错误或警告。有什么我失踪或我做错了吗?

+2

能否请您说明您作为参考样本和视频? –

回答

1

我使用CGDisplayStreamCreateWithDispatchQueuedispatch_queue_create("queue for dispatch", NULL);作为队列来解决问题。

1

对于它的价值,它看起来像你得到runloop源,但随后没有将它添加到当前的运行循环(CFRunLoopAddSource)