Post

Replies

Boosts

Views

Activity

SwiftUI using UIImagePickerController produces nil UIImage
Hi, I'm using UIImagePickerController in swiftUI through UIViewControllerRepresentable: import SwiftUI struct ImagePicker: UIViewControllerRepresentable {     @Environment(\.presentationMode) var presentationMode     @Binding var image: UIImage?     class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {         let parent: ImagePicker         init(_ parent: ImagePicker) {             self.parent = parent         }         func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {             if let uiImage = info[.originalImage] as? UIImage {                 parent.image = uiImage             }             parent.presentationMode.wrappedValue.dismiss()         }     }     func makeCoordinator() - Coordinator {         Coordinator(self)     }     func makeUIViewController(context: Context /*UIViewControllerRepresentableContextImagePicker*/) - UIImagePickerController {         let picker = UIImagePickerController()         picker.delegate = context.coordinator         return picker     }     func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context /*UIViewControllerRepresentableContextImagePicker*/) {     } } In my main view I have an optional UIImage to bind the selected image from the picker, which is displayed through a modal sheet @State private var inputImage: UIImage? ... .sheet(item: $activeSheet, onDismiss: dismissSheet) { item in             switch(item) {             case .selectPic:                 ImagePicker(image: $inputImage)             case .editPic:                 DetailedPicView(inputImage: inputImage)             }         } ... When the activeSheet variable is later changed to .editPic the DetailedPicView view is displayed through the same modal sheet. HOWEVER: this DetailedPicView view always receives a nil image, even if I chose an existing image. Please note that the onDismiss: dismissSheet function starts as follows: func dismissSheet() {         guard let inputImage = inputImage else { return } ...     } and the guard is always passed! The debugger even shows the correct picture right after this step (curiously enough, it doesn't if the breakpoint is exactly on the guard line). Later when I'll call the subview... the debugger is no more able to show the pic and the unwrap will crash. Needless to say, there is nothing else touching the input image var. I also tried to immediately switch to the other subview by simplifying the onDismiss: dismissSheet function as follows: func dismissSheet() {         guard let inputImage = inputImage else { return }          activeSheet = .editPic // -- } activating the other sheet/view, but result is the same: nil image passed. I'm using Xcode 12.4 (12D4e) Any hint ?
2
0
1.7k
Apr ’21