按下按钮即可实现摄像头切换(从前到后)

时间:2018-07-27 18:22:06

标签: ios swift camera avfoundation

触摸按钮时,我无法实现“相机开关”。 我仍然是Swift和iOS开发的新手,因此我目前实现的大多数代码都是通过教程进行的。

我尝试自行实现相机开关。 但是,这会导致此错误:

Multiple audio/video AVCaptureInputs are not currently supported

我理解该错误,但不确定如何在代码中正确实现switch功能。

这是切换/翻转功能:

@IBAction func FlipCamera(_ sender: Any) {
            captureSession.stopRunning()
            cameraPreviewlayer?.removeFromSuperlayer()
            //cameraPreviewlayer = nil
            //self.captureSession = nil

            setupCaptureSession()
            if currentCamera! == backCamera{
                //print(currentCamera)
                currentCamera = frontCamera}
            else {
                currentCamera = backCamera}
            setupCaptureSession()
            setupInputOutput()
            setupPreviewLayer()
            startRunningCaptureSession()

        }

这是整个班级:

class CameraViewController :UIViewController {

    func applyRoundCorner(_ object: AnyObject){
        object.layer.cornerRadius = (object.frame.size.width)/2
        object.layer.masksToBounds = true
    }


    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var imagePreview: UIImageView!

    @IBAction func Library(_ sender: Any) {
    }


    @IBAction func FlipCamera(_ sender: Any) {
        captureSession.stopRunning()
        cameraPreviewlayer?.removeFromSuperlayer()
        //cameraPreviewlayer = nil
        //self.captureSession = nil

        setupCaptureSession()
        if currentCamera! == backCamera{
            //print(currentCamera)
            currentCamera = frontCamera}
        else {
            currentCamera = backCamera}
        setupCaptureSession()
        setupInputOutput()
        setupPreviewLayer()
        startRunningCaptureSession()

    }


   var captureSession = AVCaptureSession()
   var backCamera: AVCaptureDevice?
   var frontCamera: AVCaptureDevice?
   var currentCamera: AVCaptureDevice?
   var cameraPreviewlayer: AVCaptureVideoPreviewLayer?
   var photoOutput: AVCapturePhotoOutput?
   var image: UIImage?


   override func viewDidLoad() {
    super.viewDidLoad()

    self.applyRoundCorner(cameraButton)
    setupCaptureSession()
    setupInputOutput()
    setupPreviewLayer()
    startRunningCaptureSession()


    }



   func setupCaptureSession(){
       captureSession.sessionPreset = AVCaptureSession.Preset.photo // Why exclamation point?
       let availableDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
       let devices = availableDevice.devices //back or front


       for device in devices {

           if device.position == AVCaptureDevice.Position.back{
                backCamera = device
           }else if device.position == AVCaptureDevice.Position.front{
                frontCamera = device
            }
            }
    currentCamera = backCamera
        }


   func setupInputOutput(){
       do {
           let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
           captureSession.addInput(captureDeviceInput)
           photoOutput = AVCapturePhotoOutput()
           photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])], completionHandler: nil)
           captureSession.addOutput(photoOutput!)
           }
       catch{
           print(error)

        }
    }

    func setupPreviewLayer(){
        cameraPreviewlayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewlayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        cameraPreviewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        cameraPreviewlayer?.frame = self.imagePreview.frame
        self.view.layer.insertSublayer(cameraPreviewlayer!, at: 0)
    }

    func startRunningCaptureSession(){
            captureSession.startRunning()
        }


    @IBAction func TakePhoto(_ sender: Any) {
        let settings = AVCapturePhotoSettings()
        photoOutput?.capturePhoto(with: settings, delegate: self)
                }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        if segue.identifier == "showPhotoSegue"{
            let previewVC = segue.destination as! PreviewViewController
            previewVC.image = self.image
    }
}

}
    extension CameraViewController: AVCapturePhotoCaptureDelegate{
        func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
            if let imageData = photo.fileDataRepresentation(){
                image = UIImage(data: imageData)
                performSegue(withIdentifier:"showPhotoSegue", sender: nil)        }
    }




}

0 个答案:

没有答案