SwiftUI / imagepicker / firebasefirestore - 图像上传正确但不显示在图像视图上

SwiftUI / imagepicker / firebasefirestore - image uploads properly but does not show on the imageview

我正在做这个项目,用户可以在其中进入设置视图并选择图像(使用图像选择器)作为个人资料图像。然后将图像上传到 Firebasefirestore,并应在同一屏幕上以圆圈形式显示。图像选择器工作并且所选图像在 firebasefirestore 上正确上传,但是所选图像未正确显示在圆圈中。你能帮帮我吗?

这是我的代码的相关部分。如有必要,很高兴分享图像选择器的代码。

import SwiftUI

struct SettingsView: View {
  @ObservedObject var settingsViewModel = SettingsViewModel()
  @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
  @State private var presented = false
  @State private var isShowPhotoLibrary = false
  @State private var image = UIImage()

var body: some View {
    NavigationView {
      VStack {
        
        Image(uiImage:self.image) // <-- here is where I want the image to appear
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 250.0, height: 250.0, alignment: .center)
            .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
            .overlay(Circle().stroke(Color.white, lineWidth: 4))
            .shadow(radius: 10)
            .scaledToFit()

        Text("Project")
          .font(.title)
          .fontWeight(.semibold)
        
        Form {
          Section {
            HStack {
              Image(systemName: "checkmark.circle")
              Text("Upload profile image")
              Spacer()
              Text("Plain")
                .onTapGesture {
                    self.isShowPhotoLibrary = true
                }
                .sheet(isPresented: $isShowPhotoLibrary) {
                    ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)
                }
                
            }
          }
          
          AccountSection(settingsViewModel: self.settingsViewModel)
        }
        
        .navigationBarTitle("Settings", displayMode: .inline)
        .navigationBarItems(trailing:
          Button(action: { self.presentationMode.wrappedValue.dismiss() }) {
            Text("Done")
        })
        
      }
        
    }
 
  }
    
}

图像选择器:

import UIKit
import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    
    var sourceType: UIImagePickerController.SourceType = .photoLibrary
    
    @Binding var selectedImage: UIImage
    @Environment(\.presentationMode) private var presentationMode

    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        
        let imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = false
        imagePicker.sourceType = sourceType
        imagePicker.delegate = context.coordinator
        
        return imagePicker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
        
    }
    
    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 {
                
                uploadprofileImage(image: image)
                
            }
            
            parent.presentationMode.wrappedValue.dismiss()
        }
    }
}

您从未设置 @Binding var selectedImage,因此 SettingsView 中的图像不会更新。

if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
    parent.selectedImage = image /// add this line!
    uploadprofileImage(image: image)        
}