gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:23:24 31 4
gpt4 key购买 nike

我正在重建 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com