我在网上浏览了一下,并自己尝试过,但实际上找不到任何关于这个主题的内容,我认为以前从未真正尝试过它。
let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }
if let captureDevice = devices.first as? AVCaptureDevice {
do {
try sessionCap.addInput(AVCaptureDeviceInput(device: captureDevice))
} catch {
}
sessionCap.sessionPreset = AVCaptureSessionPresetPhoto
sessionCap.startRunning()
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if sessionCap.canAddOutput(stillImageOutput) {
sessionCap.addOutput(stillImageOutput)
}
if let previewLayer = AVCaptureVideoPreviewLayer(session: sessionCap) {
previewLayer.bounds = view.bounds
previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
let cameraPreview = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height))
cameraPreview.layer.addSublayer(previewLayer)
cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(ViewController.saveToCamera(_:))))
view.addSubview(cameraPreview)
}
}
这就是我目前拥有的代码,基本上它的作用是在 ViewController 上显示 iPhone 后置摄像头,并且我还有其他代码允许用户拍照 - 太棒了。但是,我想知道如何可能将两个摄像机添加到同一 View 上?
黑色区域是前置摄像头(“自拍摄像头”),青色区域是后置摄像头。当这篇文章发布在这里时,我将继续寻找,但我觉得这是一个失败的原因。
最佳答案
在我看来这是不可能的。
您不能同时使用两个摄像头,因为不允许同时使用它们。因此,一旦一个 AVCaptureSession
开始,另一个 AVCaptureSession
就会停止,因为它需要设备硬件。
关于ios - 两个 iPhone 摄像头的 View 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731948/