【问题标题】:How can I preview an image with SwiftUI before uploading to firebase?在上传到 Firebase 之前,如何使用 SwiftUI 预览图像?
【发布时间】:2020-11-25 03:49:03
【问题描述】:

我关注了this tutorial,并能够成功地将图像上传到我的 Firebase 存储。

但问题是即使您点击一次它也会上传图片。没有向用户提供确认机会,我认为这不是好的用户体验。

这是整个结构

struct imagePicker : UIViewControllerRepresentable{
    
    func makeCoordinator() -> imagePicker.Coordinator {
        return imagePicker.Coordinator(parent1: self)
    }
    
    @Binding var shown : Bool
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<imagePicker>) ->
        UIImagePickerController {
            
        let imagepic = UIImagePickerController()
        imagepic.sourceType = .photoLibrary
        imagepic.delegate = context.coordinator
            
        return imagepic
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<imagePicker>) {
        
    }
    
    class Coordinator : NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

        var parent : imagePicker!
        
        init(parent1: imagePicker ){
            parent = parent1
        }
        
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            parent.shown.toggle()
        }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
            let image = info[.originalImage] as! UIImage
        
            let storage = Storage.storage()
            let storingimage = storage.reference().child("temp.jpg")
            
            let metadata = StorageMetadata()
            metadata.contentType = "image/jpeg"
            
            storingimage.putData(image.jpegData(compressionQuality: 0.35)!, metadata: metadata)
            {(response,err) in
                if(err != nil){
                    print(err?.localizedDescription)
                }
                else{
                    // You can also access to download URL after upload.
                    storingimage.downloadURL { (url, error) in
                      guard let downloadURL = url else {
                        // Uh-oh, an error occurred!
                        
                        return
                      }
                        // upload to
                       // I plan on storing this URL downloadURL
                    }
                }
            }
        }
    }
}

这是我的内容视图的 sn-p

@State var showCaptureImageView = false

...

VStack{
     Button(action: {
         self.showCaptureImageView.toggle()
     }) {
         Text("Choose Photo")
     }

     if(self.$showCaptureImageView){
         imagePicker(shown: self.$showCaptureImageView)
     
         Spacer()

         Button(action: {
             // perform imageUpload here
         }, label: {
             Text("Upload Photo")
         })
     }
}

我不知道如何创建预览,因为imagePicker 在这种情况下在UIImage 中工作,我认为它不能非常直观地呈现为Image。

【问题讨论】:

  • 非常直观Image(uiImage: _your_image_here)。参见例如stackoverflow.com/questions/58325527/…
  • @Asperi 是的,我错过了func makeCoordinator() -&gt; imagePicker.Coordinator 中的image 参数。我要写一个答案

标签: swift firebase swiftui firebase-storage


【解决方案1】:

我收到了一些提示here。基本上,我需要通过添加图像变量来修改我的makeCoordinator()。

struct imagePicker {
    @Binding var shown: Bool
    @Binding var image: Image?

    func makeCoordinator() -> imagePicker.Coordinator {
        return imagePicker.Coordinator(parent1: self, image: $image)
    }

}

在 contentView 中,我现在可以读取返回值

来自我的VStack的片段

if self.image != nil{
    self.image?.resizable()
    .frame(width: 250)
    .shadow(radius: 10)
    
}
if(self.showCaptureImageView){
    imagePicker(shown: self.$showCaptureImageView, image: self.$image)
}

在struct的正下方

@State var showCaptureImageView = false
@State var image: Image? = nil

现在,用户可以在预览中看到他们选择的照片。

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 2018-03-30
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多