0

我有一个简单的 swift 应用程序,除了输入一些数据外,用户还可以在其中从图库中选择图像。单击保存,选择的图像应上传到 Firestore 存储。我实现了此处描述的相同逻辑https://firebase.google.com/docs/storage/ios/upload-files。当我调用“.putData(data, metadata: nil)”时,什么也没有发生,也没有返回错误。这是代码片段:

    func uploadImage(imageView: UIImageView) -> String {

        // Variable for my image url
        var urlString: String
        urlString = ""

        storage = Storage.storage()

        guard let image = imageView.image,
            let imageData = image.jpegData(compressionQuality: 1.0) else {
                print ("no image uploaded")
                return ""
        }
        let imageName = String (Int64((NSDate().timeIntervalSince1970 * 1000.0).rounded())) + ".jpg"
        let imageRef = storage.reference().child(Constants.IMAGES_PATH).child(imageName)

        // QUOTE : THIS PART DOES NOT GIVE ANY FEEDBACK. NONE OF THE PRINTS INSIDE IT IS EXECUTED
        let uploadTask = imageRef.putData(imageData, metadata: nil) { (metadata, error) in
          if let error = error {
            // Uh-oh, an error occurred!
            print(error)
            return
          }
          imageRef.downloadURL { (url, error) in
            guard let downloadURL = url else {
              print ("error")
              return
            }
            urlString = downloadURL.absoluteString
            print("Downloaded image URL ", urlString)
          }
        }
        // UNQUOTE

        print ("after uploadTask", imageRef.downloadURL) // THE URL IS ALWAYS EMPTY ""

        return urlString

    }

我设置了一个调试器断点,显示uploadTask里面的代码没有被执行,只是uploadTask被执行了,里面的所有东西都被跳过了。

选择图像后调用 uploadImage 函数,如下所示:

        var image = uploadImage(imageView: imageView)

图像是在这里挑选的(这很好用):

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            imageView.contentMode = .scaleAspectFit
            imageView.image = pickedImage
            print("image chosen")
        }

        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

我是这样初始化的,应该没问题:

import UIKit
import os.log
import FirebaseFirestore
import Firebase

class DisplayMediumViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var storage: Storage!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Additional setup after loading the view.
        imagePicker.delegate = self

        // Create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(DisplayMediumViewController.imageTapped(gesture:)))

        // Aadd it to the image view;
        imageView.addGestureRecognizer(tapGesture)

        // Make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true


    }


我在互联网上搜索了很多并尝试了不同的方法来实现它,没有一个成功。如果有人提示为什么“uploadTask = imageRef.putData”不上传图像也不返回任何下载URL,那就太好了。

4

1 回答 1

0

putData是一个带有回调函数的异步调用。putData在有机会返回之前执行打印和返回。

如果你把断点放在if let error = error你应该能够看到结果。

在https://stackoverflow.com/a/57639986/556617上查看类似的问题/答案

于 2020-03-27T15:49:11.420 回答