AVCaptureDevicePosition.front不起作用

时间:2016-10-26 11:57:00

标签: swift avfoundation swift3 ios10 avcapturedevice

这是我在这里提出的第一个问题,如果没有解释清楚,请告诉我。

所以基本上我正试图用这段代码访问前置摄像头:

 captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080

    let cameraDevice = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera , mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)
   print(cameraDevice!)


    let error: NSError? = nil

    do{
    let input = try AVCaptureDeviceInput(device: cameraDevice)

        print(captureSession.canAddInput(input))

        if error == nil && captureSession.canAddInput(input){
            captureSession?.addInput(input)

            stillImageOutput = AVCaptureStillImageOutput()
            if captureSession.canAddOutput(stillImageOutput){
                captureSession.addOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                cameraView.layer.addSublayer(previewLayer!)
                stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

                captureSession?.startRunning()



            }

        }

使用该代码print(captureSession.canAddInput(input))会返回false但是当我将位置更改为后置摄像头时,一切都像魅力一样。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我不确定您使用的设备,但在iPhone 6 / 6plus上,预设分辨率对于前置摄像头来说太高了。

https://developer.apple.com/library/content/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.html

根据Apple文档,在这些设备上,最高静止图像捕获分辨率为1280 x 960.尝试使用较低的预设,看看它是否适用于前置摄像头。

这可以解释为什么它在后置摄像头上工作正常,因为它支持更高的分辨率。