metadata?.storageReference?.downloadURL() 返回 nil

4 metadata firebase swift firebase-storage

我正在尝试下载一个 url 以存储在我的数据库中,但 Firebase 中的新更新通过要求我使用新的 url 下载方式来禁止我这样做。这是我的代码:

 let imageName = NSUUID().uuidString
 let storageRef = storage.reference().child("project_images").child("\(imageName)")

    guard let uplodaData = UIImageJPEGRepresentation(image, 1) else {
        return
    }


    let uploadTask = storageRef.putData(uplodaData, metadata: nil, completion: { (metadata, error) in

        if error != nil{
            print(error ?? "Failed to upload data in the uploadImages object there was an error:", error!)
            return
        }
Run Code Online (Sandbox Code Playgroud)

如果我在这里放置一个断点,它就不会继续通过下一部分,我不知道为什么。当存储在错误的节点或其他东西时,我通常会看到这些问题。

metadata?.storageReference?.downloadURL(completion: { (url, error) in


        if error != nil {
            print("Failed to download url:", error!)
            return
        }

        let url = "\(String(describing: url))"
        uploadedImageUrlsArray.append(url)

        uploadCount += 1
        if uploadCount == imagesCount{
            completionHandler(uploadedImageUrlsArray)
        }
    })
})
Run Code Online (Sandbox Code Playgroud)

当我只是一个断点时,我可以检查完成中的错误变量,它为零。

有人可以告诉我我在这里做错了什么。

Kay*_*way 5

您为此特定图像创建了存储引用,因此请使用它而不是metadata?.storageReference.

storageRef.downloadURL(completion: { (url, error) in


    if error != nil {
        print("Failed to download url:", error!)
        return
    }

    let url = "\(String(describing: url))"
    uploadedImageUrlsArray.append(url)

    uploadCount += 1
    if uploadCount == imagesCount{
        completionHandler(uploadedImageUrlsArray)
    }
  })
})
Run Code Online (Sandbox Code Playgroud)