swift - 'Any' 不可转换为 'AVCaptureDevice' 将 Objective C 转换为 Swift 时出错

标签 swift avcapturesession google-vision

我正在重建 Google Mobile Vision“Googly Eyes”demo在 Swift 3 中。我想出了几乎所有的问题,但我仍然坚持将一个函数从 Objective C 转换为 Swift。

演示 View Controller 中的 Objective C 函数是:

  - (AVCaptureDeviceInput *)cameraForPosition:(AVCaptureDevicePosition)desiredPosition {
    BOOL hadError = NO;
    for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
      if ([device position] == desiredPosition) {
        NSError *error = nil;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                            error:&error];
        if (error) {
          hadError = YES;
          NSLog(@"Could not initialize for AVMediaTypeVideo for device %@", device);
        } else if ([self.session canAddInput:input]) {
          return input;
        }
      }
    }
    if (!hadError) {
      NSLog(@"No camera found for requested orientation");
    }
    return nil;
  }

我已将其翻译成以下内容:

func camera(for desiredPosition: AVCaptureDevicePosition) -> AVCaptureDeviceInput {
    var hadError: Bool = false
    for device: AVCaptureDevice in AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) { // ERROR ON THIS LINE
        if device.position() == desiredPosition {
            var error: Error? = nil
            let input = try? AVCaptureDeviceInput(device: device)
            if error != nil {
                hadError = true
                print("Could not initialize for AVMediaTypeVideo for device \(device)")
            }
            else if session.canAdd(input!) {
                return input!
            }
        }
    }
    if !hadError {
        print("No camera found for requested orientation")
    }
  }

我遇到的错误出现在第 3 行(for device: AVCaptureDevice in AVCaptureDevice.devices...)。错误是:“Any”不可转换为“AVCaptureDevice”。我对 Objective C 不太熟悉,之前从未使用过 AVCaptureSession,所以我一直在努力弄清楚。关于我需要如何重写这个“for device”声明有什么建议吗?

最佳答案

假设您使用最新版本的 Xcode。撰写本文时为 Xcode 8.3.3。

我向您展示了一些简化案例的代码。


案例一

继续使用已弃用的方法(不推荐):

func camera(for desiredPosition: AVCaptureDevicePosition) -> AVCaptureDeviceInput? {
    var hadError: Bool = false
    for device in AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as! [AVCaptureDevice] { //### You need to cast
        if device.position == desiredPosition {
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if session.canAddInput(input) { //###<-
                    return input
                }
            } catch {
                hadError = true
                print("Could not initialize for AVMediaTypeVideo for device \(device) with \(error)")
            }
        }
    }
    if !hadError {
        print("No camera found for requested orientation")
    }
    return nil
}

devices(withMediaType:) 的返回类型是[Any]!,所以如果你想将每个元素用作AVCaptureDevice,你需要施放它。

与您的代码类似,但我修复了一些部分,canAddcanAddInput 和错误处理。


案例2

使用 AVCaptureDeviceDiscoverySession 忽略比 10.0 更旧的 iOS 版本。

func camera(for desiredPosition: AVCaptureDevicePosition) -> AVCaptureDeviceInput? {
    var hadError: Bool = false
    for device in AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: desiredPosition).devices {
        do {
            let input = try AVCaptureDeviceInput(device: device)
            if session.canAddInput(input) {
                return input
            }
        } catch {
            hadError = true
            print("Could not initialize for AVMediaTypeVideo for device \(device) with \(error)")
        }
    }
    if !hadError {
        print("No camera found for requested orientation")
    }
    return nil
}

使用AVCaptureDeviceDiscoverySession的简单示例,.builtInWideAngleCamera在双摄像头设备上匹配前置摄像头、普通后置摄像头和广角摄像头。


注意

上述代码中的许多方法在 Swift 4/Xcode 9 中更改了它们的签名,包括返回类型。在 iOS SDK 11 中,有一个很好的方法:

class func default(AVCaptureDevice.DeviceType, for: AVMediaType?, position: AVCaptureDevice.Position)

关于swift - 'Any' 不可转换为 'AVCaptureDevice' 将 Objective C 转换为 Swift 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603879/

相关文章:

ios - 在占位符字段中创建空格

swift - Swift 枚举中的 Notification.Name 实例

swift - ARKit:通过添加 anchor 将 USDZ 模型放置在平面上,但它总是在我的头顶上方

ios - 使用 OCMock 模拟 AVCaptureDevice

android - 谷歌视觉 API : Handle native crash

android vision face 找不到耳朵

ios - SKScene 中带有 UITapGestureRecognizer 的 Swift UIPickerView?

Objective-C 到 Swift 的翻译问题

ios - 如何获取 AVCaptureDevice 的当前温度和色调?

java - 限制谷歌视觉中的检测区域,文本识别