在上传到Firebase之前,如何使用SwiftUI预览图像?

时间:2020-08-05 01:36:01

标签: swift firebase swiftui firebase-storage

我遵循了this tutorial,并能够成功将图像上传到我的Firebase存储中。

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

这是整个结构

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
                    }
                }
            }
        }
    }
}

这是我的内容视图的一部分

@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

1 个答案:

答案 0 :(得分:0)

我了解了一些提示here。基本上,我需要通过添加图片变量来修改safe_sparse_dot(X, reg.coef_.T.reshape(-1)) >>> array([-3.11195300e+01, 3.97852518e+00, 3.83392622e+01, 6.10470897e+01, -4.93104975e+00, -7.79836353e+00, -2.92253822e+01, 1.95366691e+01, -2.53890049e+01, 2.31799242e+00, -4.29133706e+01, -2.64249217e+01, ....

makeCoordinator()

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

我的struct imagePicker { @Binding var shown: Bool @Binding var image: Image? func makeCoordinator() -> imagePicker.Coordinator { return imagePicker.Coordinator(parent1: self, image: $image) } } 的片段

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

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