2012-12-24 181 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)