# How to switch camera? - Implementing a camera flip button

**URL:** <https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261>\
**Category:** iOS\
**Created:** [November 5, 2016, 5:40am UTC](https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261 "2016-11-05T05:40:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliott\_19](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elliott_19/32/46386_2.png) [@elliott\_19](https://forums.kodeco.com/u/elliott_19)\
**Post date:** [November 5, 2016, 5:40am UTC](https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261/1 "2016-11-05T05:40:08Z")

</div>

What I have figured out is that in order to switch cameras it is necessary to end the AVCaptureSession and create a new AVCapture session so you can add another AVCaptureDevice? Please correct me if I’m wrong!

How I am switching the camera right now is still really buggy, it only switches twice and the code is really long for something that seems so simple!

```
fileprivate var currentCameraPosition: CurrentCameraPosition?

enum CurrentCameraPosition
{
    case front
    case back
}

fileprivate func switchCamera()
{
    if session.isRunning
    {
        session.stopRunning()
        session.removeInput(videoDeviceInput)
    }
    
    session.beginConfiguration()
    session.sessionPreset = AVCaptureSessionPresetPhoto
    
    if currentCameraPosition == .back
    {
        do{
            let videoDeviceFrontCamera = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)
            let videoDeviceInput = try AVCaptureDeviceInput(device: videoDeviceFrontCamera)
            
            if session.canAddInput(videoDeviceInput)
            {
                session.addInput(videoDeviceInput)
                self.currentCameraPosition = CurrentCameraPosition.front
                self.videoDeviceInput = videoDeviceInput
                DispatchQueue.main.async {
                    self.cameraPreviewView.cameraPreviewLayer.connection.videoOrientation = .portrait
                }
            }
            else
            {
                print("Could not switch camera")
                return
            }
        }
        catch
        {
            print("Couldn't create video device input: \(error)")
            return
        }
    }
    else if currentCameraPosition == .front
    {
        do{
            let videoDeviceFrontCamera = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)
            let videoDeviceInput = try AVCaptureDeviceInput(device: videoDeviceFrontCamera)
            
            if session.canAddInput(videoDeviceInput)
            {
                session.addInput(videoDeviceInput)
                self.currentCameraPosition = CurrentCameraPosition.front
                self.videoDeviceInput = videoDeviceInput
                DispatchQueue.main.async {
                    self.cameraPreviewView.cameraPreviewLayer.connection.videoOrientation = .portrait
                }
            }
            else
            {
                print("Could not switch camera")
                return
            }
        }
        catch
        {
            print("Couldn't create video device input: \(error)")
            return
        }
    }
    session.commitConfiguration()
    
    sessionQueue.async {
        self.session.startRunning()
    }
}

```

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [November 7, 2016, 3:10am UTC](https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261/2 "2016-11-07T03:10:34Z")

</div>

It looks like you have a cut & paste error there - should `self.currentCameraPosition = CurrentCameraPosition.front` appear in both code paths?

Because your code paths have so much in common you should maybe extract out the common part and put it in a function that is called with an argument for front or back…

---

<div class="post-metadata">

**Author:** ![elliott\_19](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elliott_19/32/46386_2.png) [@elliott\_19](https://forums.kodeco.com/u/elliott_19)\
**Post date:** [November 8, 2016, 2:03am UTC](https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261/3 "2016-11-08T02:03:12Z")

</div>

should self.currentCameraPosition = CurrentCameraPosition.front appear in both code paths? This was the simple overlooked trick that did it! just had to set variable to the other position whenever switched!

And the method did definitely do the trick on shortening code, originally was just curious if there was a better way than having to call a method that runs through all that code again rather then just switching the position.

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [November 8, 2016, 5:12am UTC](https://forums.kodeco.com/t/how-to-switch-camera-implementing-a-camera-flip-button/22261/4 "2016-11-08T05:12:25Z")

</div>

I guess you can make incremental changes and experiment to see exactly how much setup is required when switching. Glad the suggestion helped.
