'SwiftUI AVCapturePhotoOutput Does Not Work

I have a simple SwiftUI app with SwiftUI life cycle and I am trying to use AVFoundation to automatically take a photo. I will do this based on conditions or based on a timer - but for this example, I'd just like to take a photo on startup and display it (NOT a preview layer). I do not want any user action to be required.

I clearly don't understand the proper setup and capture.

import SwiftUI
import AVFoundation

struct ContentView: View {

    let dataStore = DataStore.shared

    @State private var captureSession = AVCaptureSession()
    @State private var backCamera : AVCaptureDevice?
    @State private var frontCamera : AVCaptureDevice?

    @State private var currentCamera : AVCaptureDevice?
    @State private var photoOutput : AVCapturePhotoOutput?

    @State private var capturedImage: UIImage?

    var body: some View {
        VStack {
            Text("Take a Photo Automatically")
                .padding()

            ZStack {
                RoundedRectangle(cornerRadius: 0)
                    .stroke(Color.blue, lineWidth: 4)
                    .frame(width: 320, height: 240, alignment: .center)

                Image(uiImage: dataStore.capturedImage)
            }
            
            Spacer()
        }
        .onAppear {
        
            if UIImagePickerController.isSourceTypeAvailable(.camera){
                self.setupCaptureSession()
                self.setupDevices()
                self.setupInputOutput()
                self.startRunningCaptureSession()
            } else {
                print("No Camera is Available")
            }
        }
    }

    func setupCaptureSession() {
        captureSession.sessionPreset = AVCaptureSession.Preset.photo
    }//setupCaptureSession

    func setupDevices() {
    
        let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
    
        let devices = deviceDiscoverySession.devices
        for device in devices {
            if device.position == AVCaptureDevice.Position.back {
                backCamera = device
            } else if device.position == AVCaptureDevice.Position.front {
                frontCamera = device
            }//if else
        }//for in
    
        currentCamera = frontCamera
    
    }//setupDevices

    func setupInputOutput() {
    
        do {
            //you only get here if there is a camera ( ! ok )
            let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
            captureSession.addInput(captureDeviceInput)
            photoOutput = AVCapturePhotoOutput()
            photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: {(success, error) in
            })
            captureSession.addOutput(photoOutput!)
            captureSession.commitConfiguration()

        } catch {
            print("Error creating AVCaptureDeviceInput:", error)
        }
    
    }//setupInputOutput

    func startRunningCaptureSession() {
        let settings = AVCapturePhotoSettings()
    
        captureSession.startRunning()
        photoOutput?.capturePhoto(with: settings, delegate: PhotoDelegate())
    
    }//startRunningCaptureSession

}//struct

class PhotoDelegate: NSObject, AVCapturePhotoCaptureDelegate {

    let dataStore = DataStore.shared

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

        guard let data = photo.fileDataRepresentation(),
              let image =  UIImage(data: data)  else {
                return
        }

        dataStore.capturedImage = image
    }
}//photo delegate

class DataStore {
    static let shared = DataStore()
    private init() {}

    @Published var capturedImage: UIImage = UIImage()
}//dataStore

Any guidance would be appreciated. Xcode 12.5.1 iOS 14.5

Second attempt to add example:

enter image description here

Later edit. After correcting my approach with the changes provided by Philip Dukhov, I still had a problem - the images were still very dark - to the point that objects were unrecognizable. After a lot of trial and error, it seems that the camera needs some very small amount of time to setup before the capture. While it does not seem like a good plan for programming, I setup a slight delay before the session capture. As little as 0.1 seconds seems to be enough. Starting the capture is now:

func startRunningCaptureSession() {
    let settings = AVCapturePhotoSettings()
    captureSession.startRunning()

    //don't know why this is needed - but it works. Low number of tests at 0.1 all work
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        self.photoOutput?.capturePhoto(with: settings, delegate: self)
    }
            
}//start Running Capture Session

If you know a better way to do this - please let me know.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source