改变前后摄像头

时间:2017-01-28 22:57:27

标签: ios swift3 avfoundation

我需要将前置摄像头更换为后置摄像头。我尝试创建一个按下更换相机的按钮,但不是如何操作。这是我的代码(此代码运行前置摄像头):

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var sessionOutputSetting = AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecJPEG])

var previewLayer : AVCaptureVideoPreviewLayer?


@IBOutlet var cameraView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    previewLayer?.frame = cameraView.bounds
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)



    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)
    for device in (deviceDiscoverySession?.devices)! {
        if device.position == AVCaptureDevicePosition.back {

            do {
                let input = try AVCaptureDeviceInput(device: device)
                if captureSession.canAddInput(input) {
                    captureSession.addInput(input)

                    if captureSession.canAddOutput(sessionOutput) {
                        captureSession.addOutput(sessionOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer!)

                    }
                }

            } catch {
                print("Error")

            }
        }
    }
    captureSession.startRunning()
    print("")


}

抱歉,我还是个新手。也有人可以判断你是否使用过SwiftyCam(https://github.com/Awalz/SwiftyCam

1 个答案:

答案 0 :(得分:-1)

我已经在 swift 2 中完成了。您可以通过将语法转换为swift 3来使用它。

@IBAction func onFrontRearCameraClick(sender: AnyObject)
{
    if self.captureDevice != nil
    {
        if captureSession.running
        {
            print("captureSession running")
        }

        if btnFrontRearCamera.selected
        {
            captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Back)
        }
        else
        {
            captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Front)
        }

        if captureSession.running
        {
            //Indicate that some changes will be made to the session
            captureSession.beginConfiguration()

            let currentInput : AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput
            captureSession.removeInput(currentInput)

            do {
                try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
            } catch {
                print("Error adding video input device")
            }

            //Commit all the configuration changes at once
            captureSession.commitConfiguration()

            return
        }
    }
}