ios – 如何在Swift中获得前置摄像头?

前端之家收集整理的这篇文章主要介绍了ios – 如何在Swift中获得前置摄像头?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用实时取景的前置摄像头.我可以使用下列相机:
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
    }
}

猜你在找的iOS相关文章