我正在尝试使前置摄像头具有实时取景。我可以使用以下方法获取后置摄像头:
var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
但是我似乎找不到找到前置摄像头的方法。如何更改上面的代码以使用前置摄像头?
这是我的一个项目的工作示例,用于获取前置摄像头。这是在Objective-C中实现的,但事实证明它是可行的,并且足够容易转换为快捷方式。
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in videoDevices){ if (device.position == AVCaptureDevicePositionFront){ captureDevice = device; break; } }
在Swift 3.2+中:
if let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) { var captureDevice: AVCaptureDevice for device in videoDevices { if let device = device as? AVCaptureDevice { if device.position == AVCaptureDevicePosition.front { captureDevice = device break } } } }