在Swift 3中更改相机时录制错误

时间:2017-12-02 09:20:58

标签: ios swift camera

我是编程和学习Swift的新手。 我正在创建一个使用相机的应用程序但是当我尝试切换到前置摄像头时,当我尝试录制视频时,我收到错误线程1w:EXC_BREAKPOINT

错误发生在行

中的func startRecording()
if (connection?.isVideoOrientationSupported)!{connection?.videoOrientation = currentVideoOrientation()}

我有点困惑我的程序有这个错误。请帮助!

enum CameraType {
    case front
    case back
}

var camera = CameraType.front

func switchCamera() -> Bool {

    captureSession.stopRunning()
    previewLayer?.removeFromSuperlayer()

    captureSession = AVCaptureSession()

    captureSession.sessionPreset = AVCaptureSessionPresetHigh

    var captureDevice:AVCaptureDevice! = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)


    if (camera == CameraType.front) {
        captureDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
        camera = CameraType.back
    } else {
        captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        camera = CameraType.front
    }

    let input = try? AVCaptureDeviceInput(device: captureDevice)
    if captureSession.canAddInput(input) {
        captureSession.addInput(input)
        activeInput = input
    }


    if captureSession.canAddOutput(movieOutput) {
        captureSession.addOutput(movieOutput)
    }

    print("Abidi")

    return true

}

func reloadCamera() {

    if switchCamera() {
        setupPreview()
        startSession()
    }

    view.addSubview(dismissButton)
    dismissButton.anchor(top: view.topAnchor, left: nil, bottom: nil, right: view.rightAnchor, paddingTop: 12, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 50, height: 50)

    view.addSubview(frontCameraButton)
    frontCameraButton.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 35, height: 35)


}

func viewDidAppear(animated: Bool) {

    reloadCamera()

}

func startCapture() {

    startRecording()

}


func tempURL() -> URL? {
    let directory = NSTemporaryDirectory() as NSString

    if directory != "" {
        let path = directory.appendingPathComponent(NSUUID().uuidString + ".mp4")
        return URL(fileURLWithPath: path)
    }

    return nil
}


func startRecording() {

    if movieOutput.isRecording == false {

        let connection = movieOutput.connection(withMediaType: AVMediaTypeVideo)
        if (connection?.isVideoOrientationSupported)! {
            connection?.videoOrientation = currentVideoOrientation()
        }

        if (connection?.isVideoStabilizationSupported)! {
            connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
        }

        let device = activeInput.device
        if (device?.isSmoothAutoFocusSupported)! {
            do {
                try device?.lockForConfiguration()
                device?.isSmoothAutoFocusEnabled = false
                device?.unlockForConfiguration()
            } catch {
                print("Error setting configuration: \(error)")
            }

        }

        outputURL = tempURL()
        movieOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: self)

    }
    else {
        stopRecording()
    }

}

func stopRecording() {

    if movieOutput.isRecording == true {
        movieOutput.stopRecording()
    }
}

1 个答案:

答案 0 :(得分:0)

请勿使用力展开。而是检查isVideoOritstionSupported是否为:

  

连接?.isVideoOrientationSupported == true