AVCaptureDevicePosition.back工作得很好但是。 .front导致崩溃

时间:2017-06-18 16:51:28

标签: ios swift

我已经设置了我的应用以使用AVCaptureDevice拍摄照片。

下面的代码完美无缺,但是当我改变" .back" to" .front"我遇到了崩溃:

' NSRangeException',原因:' *** - [__ NSArray0 objectAtIndex:]:索引0超出空NSArray的界限'

let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInDualCamera], mediaType: AVMediaTypeVideo, position: .back)
    if let device = session?.devices[0] {

        if device.position == AVCaptureDevicePosition.back {

            do {

                let input = try AVCaptureDeviceInput(device: device )
                if captureSession.canAddInput(input){
                    captureSession.addInput(input)
                    stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

                    if captureSession.canAddOutput(stillImageOutput) {
                        captureSession.addOutput(stillImageOutput)
                        captureSession.startRunning()

                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer)

                        previewLayer.bounds = cameraView.frame
                        previewLayer.position = CGPoint(x: cameraView.frame.width / 2, y:cameraView.frame.height / 2)
                    }

                }

            }                catch {


            }
        }
    }

0 个答案:

没有答案