将ImageCaptureCore与pyobjc

时间:2017-04-18 23:48:11

标签: macos pyobjc

我正在尝试测试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的理解,我应该在didEnumerateLocalDevices完成获取设备列表后获得ICDeviceBrowser回调(即使没有设备插入),我应该得到{每个连接的设备{1}}。相反,我什么都没得到(无论我是否插入设备,无论是在我开始编写脚本之前还是之后;我尝试过普通的USB相机以及iPhone):

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

是否需要进行任何其他类型的设置才能让didAddDevice开始工作?我已经查看了本机objc示例,并且看起来就像我正在做同样的事情,但我无法找到任何可以跟随的pyobjc绑定示例

1 个答案:

答案 0 :(得分:0)

这是需要进入while循环的银弹:

build.sbt