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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…