How can I switch between 11 pro cameras using AVFoundation in Xcode

Viewed 1113

I've just started out learning Swift in Xcode and am creating a simple camera App to get up and running. I have a button that switches between the front and back facing cameras working but want to add the option to also switch between the Tele and the Ultra Wide lens on the iPhone 11 Pro.

I have created the functions to run a new CaptureSession for each lens (if detected) but was just wondering how I can call these functions to UIbutton function

The thing that's got me scratching my head is the if statement used to switch between the front and back camera says if input.device.position == .back {

This only specificities if it's front or back, not the lens itself. What would be an efficient way to make the button then Change from the front to wide, the Tele, the Ultra Wide and back to the front each time the button is pressed?

Apologies for any misuse of terminology, I'm very new to coding in Swift. Thank you!

    {
        guard let CurrentCameraInput: AVCaptureInput = CaptureSession?.inputs.first else {
            return
        }

        if let input = CurrentCameraInput as? AVCaptureDeviceInput

        {



            if input.device.position == .back {


            SwitchToFrontCamera() }


            if input.device.position == .front {

            SwitchToBackCamera()

              }



            }
        } 
1 Answers

Welcome!

Check you this initializer for AVCaptureDevice. You can specify the DeviceType you want to use, like .builtInUltraWideCamera or .builtInTelephotoCamera.

You can use a AVCaptureDevice.DiscoverySession to get a list of all capture devices available to your app.

Related