Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

uiimage - SwiftUI - Unbalanced Calls To Begin/End Appearance Transitions For

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...