仅在iPhone 7之前的设备上使用前置摄像头时崩溃

时间:2018-11-08 17:22:36

标签: ios swift avfoundation avcapturesession

我最近开始在基于相机的应用程序上运行Beta。除了在iPhone 6设备上,一切都按预期进行。

会话在后置摄像头上开始,并且每次iPhone 6用户切换到前摄像头时,应用程序都会崩溃。 (而且要非常清楚:在其他任何iPhone机型上都没有人遇到此问题。)我已经尝试使用6进行测试,并且可以始终如一地重现该错误,从而导致libc++abi.dylib: terminating with uncaught exception of type NSException

我尝试在前置摄像头上启动会话,但该会话立即崩溃。

func initializeCamera() {
    self.captureSession.sessionPreset = .hd1920x1080

    let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera],
                                                          mediaType: .video,
                                                          position: .unspecified) as AVCaptureDevice.DiscoverySession

    for device in discovery.devices as [AVCaptureDevice] {
        if device.hasMediaType(.video) {
            if device.position == AVCaptureDevice.Position.front {
                videoCaptureDevice = device
                do {
                    try currentDeviceInput = AVCaptureDeviceInput(device: device)
                } catch {
                    print("error: \(error.localizedDescription)")
                }
            }
        }
    }

    if videoCaptureDevice != nil {
        do {
            let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice!)
            captureSession.addInput(videoInput)

            if let audioInput = AVCaptureDevice.default(for: .audio) {
                try captureSession.addInput(AVCaptureDeviceInput(device: audioInput))
            }

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

            guard let previewLayer = previewLayer else { return }

            cameraPreviewView.frame = cameraContainer.frame

            cameraPreviewView.layer.addSublayer(previewLayer)
            previewLayer.frame = cameraPreviewView.frame

            previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

            setVideoOrientation()

            captureSession.addOutput(movieFileOutput)

            if let movieFileOutputConnection = movieFileOutput.connection(with: .video) {
                if movieFileOutputConnection.isVideoStabilizationSupported {
                    movieFileOutputConnection.preferredVideoStabilizationMode = .cinematic
                }
            }

            captureSession.startRunning()

            sessionIsReady(true)

        } catch {
            print("error: \(error.localizedDescription)")
        }
    }

}
func setVideoOrientation() {
    if let connection = self.previewLayer?.connection {
        if connection.isVideoOrientationSupported {
            connection.videoOrientation = .portrait
            previewLayer?.frame = cameraContainer.bounds
        }
    }
}

崩溃在captureSession.addInput(videoInput)处触发。 videoInput不是nil。相机的方向锁定为纵向。

谁能提供任何见解?请让我知道是否还有其他代码会有所帮助。预先感谢。

1 个答案:

答案 0 :(得分:3)

captureSession.addInput(videoInput)导致崩溃。

因此,在避免崩溃之前,您应该使用canAddInput(_:)

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}

在您的情况下,该iPhone 6的captureSession.canAddInput(videoInput) ==否。

现在,您也在做self.captureSession.sessionPreset = .hd1920x1080

但是according to WikiPedia,iPhone 6前置摄像头硬件支持 相机1.2 MP(最大1280×960像素),720p视频录制(30 fps)。似乎不适合1920 * 1080(“全高清”)。

您可以执行此操作以检查可以使用的“最大” AVCaptureSession.Preset

func setSessionPreset(forDevice device: AVCaptureDevice) {
    let videoPresets: [AVCaptureSession.Preset] = [.hd4K3840x2160, .hd1920x1080, .hd1280x720] //etc. Put them in order to "preferred" to "last preferred"
    let preset = videoPresets.first(where: { device.supportsSessionPreset($0) }) ?? .hd1280x720
    captureSession.sessionPreset = preset
}