Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 切换前/后摄像头_Ios_Swift_Xcode_Camera - Fatal编程技术网

Ios 切换前/后摄像头

Ios 切换前/后摄像头,ios,swift,xcode,camera,Ios,Swift,Xcode,Camera,在我上一个问题之后,我对代码做了很多修改,增加了一组新问题,这就是为什么我决定创建一个新问题,而不是遵循上一个问题 我目前正在使用Swift development,我正在尝试制作一个基本的应用程序,在UIImageview中显示摄像头 我使用的是AVFoundation框架 到目前为止,我已经能够在负载上设置前置摄像头。我的问题是,我将如何实现在点击按钮时切换相机的功能 我首先初始化实例: var captureSession = AVCaptureSession() var sessionO

在我上一个问题之后,我对代码做了很多修改,增加了一组新问题,这就是为什么我决定创建一个新问题,而不是遵循上一个问题

我目前正在使用Swift development,我正在尝试制作一个基本的应用程序,在
UIImageview
中显示摄像头

我使用的是
AVFoundation
框架

到目前为止,我已经能够在负载上设置前置摄像头。我的问题是,我将如何实现在点击按钮时切换相机的功能

我首先初始化实例:

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG])
var previewLayer = AVCaptureVideoPreviewLayer()
我还创建了一个切换布尔:

// Bool to manage camera toggle. False = front-face (default)
var toggle = false
视图willapear
中,我调用
pickCamera
函数,该函数检查切换值并创建设备描述会话:

func pickCamera(which: Bool) {
    if (which == true) {
        let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera, AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)

        startCamera(deviceDesc: deviceDescovery!)
        toggle = true;

    } else if (which == false) {
        let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera, AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)

        startCamera(deviceDesc: deviceDescovery!)
        toggle = false;

    }
}
然后,
startCamera
函数创建并设置captureSession,并添加到父层以显示:

func startCamera(deviceDesc: AVCaptureDeviceDiscoverySession) {
    for device in (deviceDesc.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
                        captureSession.startRunning()
                        cameraView.layer.addSublayer(previewLayer)
                    }
                }
            }
            catch {
                print("Exception")
            }
        } else if (device.position == AVCaptureDevicePosition.front) {
            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
                        captureSession.startRunning()
                        cameraView.layer.addSublayer(previewLayer)
                    }
                }
            }
            catch {
                print("Exception")
            }
        }
    }

}
我还添加了一个带有操作的按钮:

@IBAction func toggleCamera(_ sender: Any) {
    if (toggle == false) {
        print("Changing to back camera")
        previewLayer.removeFromSuperlayer()
        toggle = true;
        pickCamera(which: true)

    } else if (toggle == true) {
        print("Changing to front camera")
        previewLayer.removeFromSuperlayer()
        toggle = false;
        pickCamera(which: false)
    }
}
toggle方法应该清除当前视图并调用
pickCamera
方法,该方法应该创建备用相机的新实例

尽管出于某种原因,这是行不通的。我猜这与没有正确地清除以前的视图/添加新视图有关,但我还是不确定

感谢您抽出时间来研究我的问题,请询问我是否缺少信息或没有正确解释自己

更新 终于修好了。问题在于在创建新的
captureSession
之前没有停止当前的
captureSession

为了解决这个问题,我更新了
toggleCamera
功能,包括:

let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput

对于任何对代码感兴趣的人,请查看

请不要删除问题以再次发布相同的问题。最好编辑原始问题。道歉@shallowthough。我以后会考虑的。如果您能看一下这个问题,我将非常感谢。我遵循了您之前的评论并将其外部化为一个单独的函数,但我似乎在检查是否可以添加输入方面遇到了问题。
toggleCamera()
是存根的(没有实现)。抱歉,我不理解。你能解释一下吗?我想我只需要
toggleCamera()
就可以调用
pickCamera()
哪个应该完成所有的工作?