0

I have a .onTapGesture modifier that when tapped presents an ImagePicker before immediately dismissing it.


@State private var updateInfo = false

var body: some View {
  HStack {
            placeholder.image
                .resizable()
                .aspectRatio(contentMode: .fill)
                .font(.system(size: 16, weight: .medium))
                .frame(width: 100, height: 100)
                .clipShape(Circle())
                .shadow(color: Color.black.opacity(0.1), radius: 1, x: 0, y: 1)
                .onTapGesture {
                    updateInfo.showImagePicker = true
                }
                .sheet(isPresented: $updateInfo.showImagePicker) {
                    ImagePicker(showImagePicker: $updateInfo.showImagePicker, pickedImage: $updateInfo.image, imageData: $updateInfo.imageData)
                }
        }
    }

Here's my ImagePicker

import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    @Binding var showImagePicker: Bool
    @Binding var pickedImage: Image
    @Binding var imageData: Data
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = context.coordinator
        return imagePicker
        
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
        return
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
        var parentImagePicker: ImagePicker
        
        init(_ imagePicker: ImagePicker) {
            self.parentImagePicker = imagePicker
        }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
            let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
            
            parentImagePicker.pickedImage = Image(uiImage: uiImage)
           
            if let mediaImage = uiImage.jpegData(compressionQuality: 0.5) {
                parentImagePicker.imageData = mediaImage
            }
            parentImagePicker.showImagePicker = false
        }
        
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            parentImagePicker.showImagePicker = false
        }
    }
}

I keep getting this error in the console: Unbalanced calls to begin/end appearance transitions for <_UIImagePickerPlaceholderViewController: 0x...>

Not sure where I'm transitioning incorrectly. I have a tab bar view with three tabs. The final tab has a navigation bar item that pushes the detail updateInfo view.

I tap the placeholder image and system presents the image picker controller (kind of) before immediately dismissing. I tap it again and the image picker controller presents.

Thoughts on why it dismisses the first time?

David
  • 333
  • 1
  • 2
  • 14
  • This is SwiftUI internals, if you app behavior is not affected just igore it. – Asperi Jan 07 '21 at 05:13
  • @Asperi this is bad UX. When you transition to the `updateInfo` view, your current info is prepopulated in textfields. When you tap the placeholder image to, say, update your photo, the image picker dismisses and resets the fields, effectively erasing them. – David Jan 07 '21 at 05:36
  • @Asperi it wants to begin anew, but at the laborious expense of making the user retype the values they don't want to update. – David Jan 07 '21 at 05:37
  • Needed reproducible example, provided code snapshot does not have any text fields. – Asperi Jan 07 '21 at 06:50
  • It's not about the textfields. It's the fact that when I tap the image, the picker dismisses and needs to be tapped twice to allow for image selection. – David Jan 07 '21 at 20:34

1 Answers1

0

A solution that is not exactly a solution

I was trying to call the ImagePickerController on a view and kept getting the unbalanced views error. So I created a different view entirely for picking a new image. The error has not been addressed, but the issue for me is solved.

E_net4
  • 27,810
  • 13
  • 101
  • 139
David
  • 333
  • 1
  • 2
  • 14