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

我正试图让现场观看的前置摄像头。 我能够使用后面的相机:

var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

但我似乎无法find如何获得前置摄像头。 如何更改上面的代码以使用前置摄像头?

从我的一个项目中获得前置摄像头,这是一个有效的例子。 这是在Objective-C,但certificate工作,并很容易转换到迅速。

 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 } } } } 
 guard let device = AVCaptureDevice.devices().filter({ $0.position == .Front }) .first as? AVCaptureDevice else { fatalError("No front facing camera found") } 

如果你正在寻找一个较短的解决scheme,虽然.filter后面.filter不是最有效的

在iOS 10.0和更高版本中,您不需要通过AVCaptureDevice进行迭代。 devices (或devicesWithMediaType )按位置查找摄像头。 (实际上,这两个API在iOS 10中都被弃用,并且不会返回iPhone 7 Plus,iPhone 8 Plus或iPhone X上的全部可用设备。

如果您只需要根据简单的特征(如可以拍摄video的前置摄像头)find单个设备,则只需使用AVCaptureDevicedefaultDevice(withDeviceType:mediaType:position:) 。 例如:

 guard let device = AVCaptureDevice.defaultDevice( withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) else { fatalError("no front camera. but don't all iOS 10 devices have them?") // then use the device: captureSession.addInput(device) or whatever 

真的,这就是大部分用例所需要的。


还有AVCaptureDeviceDiscoverySession作为迭代devices数组的旧方法的替代品。 然而,通常使用新的defaultDevice(withDeviceType:mediaType:position:)方法可以find大部分通常在devices数组中迭代的东西,所以您可以使用它并编写更less的代码。

AVCaptureDeviceDiscoverySession值得使用的情况是不常见的,更复杂的情况:假设你想查找所有支持特定帧速率的设备,或者使用键值观察来查看可用设备集何时改变。


顺便说一句,苹果公司也有一个 iOS 10 / Swift 3照片捕获系统的指南和一些示例代码 ,这些代码都展示了这些API的最新实践。

Swift 4,iOS 10+和Xcode 9 beta 5取而代之

 if let cameraID = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)?.localizedName { //cameraID = "Front Camera" } 

AVCaptureDevice.DiscoverySession实现

 if #available(iOS 10,*){ if let cameraID = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front).devices.first?.localizedName{ //cameraID = "Front Camera" } } 

最新的是:

 guard let device = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: .video, position: .front) else { return 

}

基于mbuff24提供的解决scheme。 数组方法包括.first(where :)除了.filter()

 guard let frontCamera = AVCaptureDevice.devices().first(where: { ($0 as AnyObject).position == .front }) as? AVCaptureDevice else { fatalError("No front facing camera found") } 
 import AVFoundation import Foundation @IBOutlet weak var imageView: UIImageView! let captureSession = AVCaptureSession() let stillImageOutput = AVCaptureStillImageOutput() var previewLayer : AVCaptureVideoPreviewLayer? var captureDevice : AVCaptureDevice? var Arrdata:[Studentdata] = [] override func viewDidLoad() { super.viewDidLoad() captureSession.sessionPreset = AVCaptureSessionPresetHigh if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] { for device in devices { if (device.hasMediaType(AVMediaTypeVideo)) { if(device.position == AVCaptureDevicePosition.front) { captureDevice = device if captureDevice != nil { print("Capture device found") beginSession() } } } } } } func beginSession() { do { try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice)) stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG] if captureSession.canAddOutput(stillImageOutput) { captureSession.addOutput(stillImageOutput) } } catch { print("error: \(error.localizedDescription)") } guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else { print("no preview layer") return } self.view.layer.addSublayer(previewLayer) previewLayer.frame = self.view.layer.frame captureSession.startRunning() self.view.addSubview(imageView) }