由于AVCaptureDevice.devices在iOS 10中已贬值,因此我尝试将此示例代码调整为AVCaptureDeviceDiscoverySession。
var error: NSError?
var captureSession: AVCaptureSession?
var backVideoDevice: AVCaptureDevice?
//let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) // .devices DEPRECIATED
//iOS 10
let videoDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back)
// Get back video device
if let videoDevices = videoDevices
{
for captureDevice in videoDevices
{
if (captureDevice as AnyObject).position == AVCaptureDevicePosition.back
{
backVideoDevice = captureDevice as? AVCaptureDevice
break
}
}
}
在这里我卡住了,在这条线上出现了一个错误
for captureDevice in videoDevices
此时videoDevices显示:键入“AVCaptureDeviceDiscoverySession”不符合协议“Sequence”。
我错过了什么或监督了什么?谢谢。
最佳答案
该函数返回一个AVCaptureDeviceDiscoverySession
类型的对象,您必须访问该对象的devices
属性才能获取所需的数组:
let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back)
if let device = session?.devices[0] {
backVideoDevice = device
}
注意,您不再需要在所有设备上循环,因为
AVCaptureDeviceDiscoverySession
只返回一开始position
为.back
的设备。因为只有一个,你可以在devices[0]
找到它。关于ios - AVCaptureDevice.devices调整为AVCaptureDeviceDiscoverySession,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41904375/