簡體   English   中英

使用CGDisplayStream捕獲屏幕

[英]Capture screen with CGDisplayStream

我正在嘗試在我的應用程序中實現一個將記錄屏幕的功能。 我在一些示例代碼和WWDC 2012視頻中找到了一些代碼。

到目前為止,我有這個。

- (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的回調塊。 我沒有收到任何錯誤或警告。 有什么東西我缺少或我做錯了嗎?

我通過使用CGDisplayStreamCreateWithDispatchQueue並傳遞dispatch_queue_create("queue for dispatch", NULL);解決了這個問題dispatch_queue_create("queue for dispatch", NULL); 作為隊列。

對於它的價值,看起來你正在獲得runloop源,但是然后沒有將它添加到當前的運行循環(CFRunLoopAddSource)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM