我正在尝试使用实时取景的前置摄像头.我可以使用下列相机:
var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
但我似乎找不到如何获得前置摄像头.如何更改上述代码以使用前置摄像头?
解决方法
这是我的一个项目的工作示例,以获得前置摄像头.这是客观的,但被证明是工作和容易转换成迅速.
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in videoDevices){ if (device.position == AVCaptureDevicePositionFront){ captureDevice = device; break; } }
在Swift(未测试)中:
let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) var captureDevice:AVCaptureDevice for device in videoDevices{ let device = device as AVCaptureDevice if device.position == AVCaptureDevicePosition.Front { captureDevice = device break } }