import SwiftUI import UIKit extension ImagePicker: UIViewControllerRepresentable { var sourceType: UIImagePickerController.SourceType { return .photoLibrary } func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIImagePickerController { let imagePicker = UIImagePickerController() imagePicker.allowsEditing = false imagePicker.sourceType = sourceType imagePicker.delegate = context.coordinator return imagePicker } func updateUIViewController( _ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext ) { } 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() } } }