使用ICDeviceBrowser无法找到相机设备

问题描述 投票:0回答:1

当我使用ICDeviceBrowser检测通过电缆链接的相机时,第一次可以检测到相机设备,但是当我退出页面并再次进入页面时,我无法检测到链接的相机设备。这是代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addImageCaptureCore];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self checkCameraConnection];
    });
}

- (void)checkCameraConnection {
    if (@available(iOS 13.0, *)) {
        NSArray<ICDevice *> *connectedDevices = self.browser.devices;
        
        if (connectedDevices.count > 0) {
            NSLog(@"Camera is connected");
        } else {
            NSLog(@"Camera is not connected");
        }
    } 
    else {
        // Fallback on earlier versions
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (@available(iOS 13.0, *)) {
        if (self.cameraDevice) {
            if (self.cameraDevice.hasOpenSession) {
                [self.cameraDevice requestCloseSession];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.browser stop];
                    self.browser.delegate = nil;
                    self.browser = nil;
                });
            }
            else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.browser stop];
                    self.browser.delegate = nil;
                    self.browser = nil;
                });
            }
        }
    } else {
        // Fallback on earlier versions
    }
}

- (void)addImageCaptureCore {
    if (@available(iOS 13.0, *)) {
        ICDeviceBrowser *browser = [[ICDeviceBrowser alloc] init];
        browser.delegate = self;
        [browser start];
        self.browser = browser;
    } 
    else {
        
    }
}

#pragma mark - ICDeviceBrowserDelegate
    
- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)device moreComing:(BOOL) moreComing  API_AVAILABLE(ios(13.0)){
    NSLog(@"Device name = %@",device.name);
    
    if ([device isKindOfClass:[ICCameraDevice class]]) {
        if ([device.capabilities containsObject:ICCameraDeviceCanAcceptPTPCommands]) {
            ICCameraDevice *cameraDevice = (ICCameraDevice *)device;
            cameraDevice.delegate = self;
            [cameraDevice requestOpenSession];
            self.cameraDevice = cameraDevice;
        }
    }
    
}

- (void)deviceBrowser:(ICDeviceBrowser*)browser didRemoveDevice:(ICDevice*)device moreGoing:(BOOL) moreGoing  API_AVAILABLE(ios(13.0)){
    if (self.cameraDevice) {
        if (self.cameraDevice.hasOpenSession) {
            [self.cameraDevice requestCloseSession];
            self.cameraDevice.delegate = nil;
            self.cameraDevice = nil;
        }
        else {
            self.cameraDevice.delegate = nil;
            self.cameraDevice = nil;
        }
    }
}

#pragma mark - ICCameraDeviceDelegate

- (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray<ICCameraItem*>*) items  API_AVAILABLE(ios(13.0)){
    if (items.count > 0) {
        ICCameraItem *latestItem = items.lastObject;
        NSLog(@"name = %@",latestItem.name);
    }
    
}

#pragma mark - ICDeviceDelegate

- (void)device:(ICDevice*)device didOpenSessionWithError:(NSError* _Nullable) error  API_AVAILABLE(ios(13.0)){
    if (error) {
        NSLog(@"Failed to open session %@",error.localizedDescription);
    }
    else {
        NSLog(@"open session success");
    }
}

- (void)device:(ICDevice*)device didCloseSessionWithError:(NSError* _Nullable)error  API_AVAILABLE(ios(13.0)){
    if (error) {
        NSLog(@"close session error = %@",error.localizedDescription);
    }
    else {
        NSLog(@"didCloseSession");
    }
}

- (void)didRemoveDevice:(ICDevice*)device {
    
}

如何确保退出页面重新进入页面时能够检测到摄像头设备

ios image-capture
1个回答
0
投票

以下是我想到的一些建议:

  1. 验证控制器是否第二次初始化 - 可能是它有一个未释放的引用,因此不会取消初始化。您也可以检查
    viewDidLoad
    是否被第二次调用。
  2. 与第 1 点相关,您可以显式停止设备浏览器,例如
- (void)dealloc {
    [self.deviceBrowser stop];
}

再次确保

dealloc
是否被调用

  1. 在列出设备时,您会指望设备列表中包含设备,但不可能每次都如此。为什么?因为在第一次调用委托方法
    .devices
    之前,
    deviceBrowser(_:didAdd:moreComing:)
    数组是空的。因此,您尝试在第二次实际检测到设备之前列出设备可能是一种竞争条件(可能再次与前两点相关。我的建议是对许多可能的逻辑之一进行轻微的更改)方法:a) 在视图出现后开始执行初始化和检测,或者 b) 仍然在
    viewDidLoad
    中初始化浏览器,但在列出设备之前等待
    deviceBrowser(_:didAdd:moreComing:)
    被触发,或者 c) 定义一个属性
    @property (assign, nonatomic) BOOL hasAddedAllDevice;
    并一旦
    didAddDevice
    返回
    moreComing
    NO
    .
  2. 将其设置为 true
© www.soinside.com 2019 - 2024. All rights reserved.