How to record video in SwiftUI and UIImagePickerController

Viewed 50

I have been researching for quite a while on how I can create a camera app where the user can take a picture while taking a video (much like in the default camera app). However, I am not able to find any way to achieve this functionality using a UIImagePickerController.

This is my current UIImagePickerController which can only take pictures.

struct ImagePicker: UIViewControllerRepresentable {
    
    @Binding var isShown: Bool
    @Binding var image: UIImage?
    
    @Binding var didTapCapture: Bool
    
    @Binding var flash: Bool?
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
        uiViewController.showsCameraControls = false
        if (didTapCapture == true){
                uiViewController.takePicture()
        
            
                didTapCapture = false
            }
    }
    
    func makeCoordinator() -> ImagePickerCoordinator {
        return ImagePickerCoordinator(isShown: $isShown, image: $image)
    }
    
    func toggleFlash() {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
        guard device.hasTorch else { return }

        do {
            try device.lockForConfiguration()

            if (device.torchMode == AVCaptureDevice.TorchMode.on) {
                device.torchMode = AVCaptureDevice.TorchMode.off
            } else {
                do {
                    try device.setTorchModeOn(level: 1.0)
                } catch {
                    print(error)
                }
            }

            device.unlockForConfiguration()
        } catch {
            print(error)
        }
    }
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        self.toggleFlash() // does not work
        picker.delegate = context.coordinator
        picker.sourceType = .camera
        picker.showsCameraControls = false
        picker.modalPresentationStyle = .fullScreen

        return picker
    }
    
}

How can I incorporate the necessary functionality?

Thanks

0 Answers
Related