Image Picker using Camera in Landscape not working properly (SwiftUI)

Viewed 403

My Image Picker does work with the Photo library in both orientations, but using the camera it doesn't adjust correctly to landscape.

This is how it looks: Landscape: Problematic Camera View Portrait: Camera View That Looks Normal

Here is My Code:

File1, UIViewControllerRepresentable:

import UIKit
import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {

var sourceType: UIImagePickerController.SourceType = .photoLibrary

@Binding var selectedImage: UIImage
@Environment(\.presentationMode) private var presentationMode

func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
    
    let imagePicker = UIImagePickerController()
    imagePicker.allowsEditing = true
    imagePicker.sourceType = sourceType
    imagePicker.delegate = context.coordinator
    
    return imagePicker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
    
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}



final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    
    var parent: ImagePicker
    
    init(_ parent: ImagePicker) {
        self.parent = parent
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            parent.selectedImage = image
        }
        
        parent.presentationMode.wrappedValue.dismiss()
    }
}
}

File 2, Using It:

.fullScreenCover(isPresented: $isShowPhotoLibrary) {
            ImagePicker(sourceType: .camera, selectedImage: self.$image)
        }

I am not fixed on using a fullScreenCover, just did that because it worked at least partially.

If anyone could help I would appreciate it a lot.

1 Answers
Related