简体   繁体   English

尝试使用AVFoundation从前置摄像头拍摄照片时出现致命错误

[英]Fatal Error when trying to capture photo from front-facing camera using AVFoundation

I try to implement code which will allow to capture images from front-facing camera and then share them. 我尝试实现代码,允许从前置摄像头捕获图像,然后共享它们。 However, each time I try to "take a photo", my app crashes with following console warning: 但是,每次我尝试“拍照”时,我的应用程序都会出现以下控制台警告:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inconsistent state.'
*** First throw call stack:
(0x181b1ee38 0x181183f80 0x188265074 0x10003d578 0x10003d748 0x10003c380 0x10003c3d8 0x186ca0ad0 0x186ca0a4c 0x186c88740 0x186ca033c 0x186c58b0c 0x186c994f8 0x186c98af4 0x186c68f4c 0x186c67528 0x181ad5124 0x181ad4bb8 0x181ad28b8 0x1819fcd10 0x1832e4088 0x186cd1f70 0x100036880 0x18159a8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

I'm posting my code below: 我在下面发布我的代码:

import UIKit
import AVFoundation

class CameraSnapViewController: UIViewController {

    let captureSession = AVCaptureSession()
    let stillImageOutput = AVCaptureStillImageOutput()

    let cameraPicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.addPhotoCapturingFunctionality()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func cameraButtonPressed(sender: UIButton) {
        self.takePhoto()
    }

}

extension CameraSnapViewController {
    func addPhotoCapturingFunctionality() {
        guard let device = AVCaptureDevice.devices().filter({ $0.hasMediaType(AVMediaTypeVideo) && $0.position == .Front }).first as? AVCaptureDevice else { fatalError("No front facing camera found") }

        do {
            let input = try AVCaptureDeviceInput(device: device)
            captureSession.addInput(input)
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto
            stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
            if captureSession.canAddOutput(stillImageOutput) {
                captureSession.addOutput(stillImageOutput)
            }
        } catch let error as NSError {
            print(error)
        }

    }

    func takePhoto() {
        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {

            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageDataSampleBuffer, error) -> Void in
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                // TODO: Sharing
            }

        }
    }
}

How can I fix the issue? 我该如何解决这个问题?

You have to start session before you take a photo. 您必须在拍照前开始会话。

captureSession.startRunning()

😜 😜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM