2017-04-18 52 views
0

我想考出pyobjc綁定ImageCaptureCore,我不能讓我的委託的回調在所有火:使用ImageCaptureCore與pyobjc

 
"""ImageCaptureCore test app.""" 

from __future__ import (
    absolute_import, 
    division, 
    print_function, 
) 

import time 

import objc 
from AppKit import NSObject 
from ImageCaptureCore import (
    ICCameraDevice, 
    ICDeviceBrowser, 
    ICDeviceLocationTypeMaskLocal, 
    ICDeviceTypeMaskCamera, 
) 

class ICDelegate(NSObject): 
    """Implements ICDeviceBrowserDelegate.""" 

    def __new__(cls): 
     """Create a new `ICDelegate`.""" 
     return ICDelegate.alloc().init() 

    def init(self): 
     self = objc.super(ICDelegate, self).init() 
     if self is None: 
      return None 
     self.devicesFetched = False 
     print("Delegate initialized.") 
     return self 

    # ICDeviceBrowserDelegate callbacks 
    def deviceBrowserDidEnumerateLocalDevices_(self, browser): 
     print("Done fetching devices.") 
     self.devicesFetched = True 

    def deviceBrowser_didAddDevice_moreComing_(self, browser, device, moreComing): 
     print("Device added.") 

    def deviceBrowser_didRemoveDevice_moreGoing_(self, browser, device, moreGoing): 
     print("Device removed.") 

delegate = ICDelegate() 
browser = ICDeviceBrowser.alloc().init() 

browser.setDelegate_(delegate) 
browser.setBrowsedDeviceTypeMask_(ICDeviceTypeMaskCamera | ICDeviceLocationTypeMaskLocal) 

browser.start() 
print("Fetching cameras...") 
while not delegate.devicesFetched: 
    time.sleep(1) 
    print("Still fetching cameras...") 
    # We never get past this! 

從我的API的理解,一旦ICDeviceBrowser完成獲取設備列表(即使沒有設備插入),我應該得到didEnumerateLocalDevices回撥,並且我應該爲每個連接的設備獲得didAddDevice。相反,我得到什麼(不管我是否插上的設備或沒有,之前或之後,我開始我的劇本,我已經嘗試了普通的USB攝像頭以及一個iPhone):

 
Delegate initialized. 
Fetching cameras... 
Still fetching cameras... 
Still fetching cameras... 
Still fetching cameras... 
Still fetching cameras... 

有什麼其他類型的設置需要發生才能使ICDeviceBrowser開始工作?我看過本地objc示例,它看起來看起來就像我正在做同樣的事情,但我一直無法找到任何可以跟隨的pyobjc綁定示例。

回答

0

這是靈丹妙藥,需要在while循環中去:

while True: 
    NSRunLoop.currentRunLoop().runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1))