【问题标题】:I try to get the url of the image I uploaded to storage, the codes I find are very old我尝试获取我上传到存储的图像的 url,我发现的代码很旧
【发布时间】:2020-05-06 23:25:16
【问题描述】:

我尝试获取我上传到存储的图像的 url (https://firebasestorage.googleapis.com/v0/b/mismascotasgb-oficial.appspot.com/o/petImage%2Fq5ABEFEWZNX7PqTJZ9Nm.jpg?alt=media&token=12b8c0b7-d65a -441c-a2d8-cfb55cf8498d)。

我在网上搜索了很多,但我只找到了旧出版物,我发现最近的一个向我显示了这段代码,但是当我执行它时它没有进入,代码的内部部分被跳过了。

        let db = Firestore.firestore()
        let newDocument = db.collection("pets").document()
        pid = newDocument.documentID

        pid = newDocument.documentID
        let pidJPG = pid + ".jpg"

        let storageRef = RefStorage.petImages.reference().child(pidJPG)

        if let uploadData = self.imagePet?.jpegData(compressionQuality: 0.2){

            storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
                if error != nil {
                    print("error: \(String(describing: error))")
                    return
                }
            }

            urlImagePet = storageRef.description
            storageRef.downloadURL(completion: {(url, error) in
                if error != nil {
                 print(error!.localizedDescription)
                   return
                }
                self.image = url!.absoluteString
            })
        }
        db.collection("pets").document(pid).setData(toDictionary()) { err in
            if let err = err {
                print("\n--------------------------------------")
                print("Error writing document: \(err)")
                print("--------------------------------------\n")
            } else {
                print("\n--------------------------------------")
                print("Document successfully written!")
                print("--------------------------------------\n")
            }
        }
    }



func toDictionary() -> [String : Any]{
        return[
            "pid" : pid,
            "urlImagePet" : urlImagePet,
            "image" : image
        ]

    }

【问题讨论】:

  • urlImagePet = storageRef.description save (gs://....),但我需要 (https://...),我只是尝试不同的选项,我需要两个 urlImagePet (gs://) 和我需要的图片 (https://)
  • 如果你打印storageRef.downloadURL(completion:完成处理程序url!.absoluteString,你会得到URL吗?
  • 不,我不能在里面打印,@FrankvanPuffelen,你能告诉我这两个网址的区别吗?

标签: swift image firebase url firebase-storage


【解决方案1】:

putDatadownloadURLsetData 的调用都是异步的。这意味着这些调用的结果仅在其完成处理程序中可用。要正确使用这些结果,您需要嵌套调用,就像您已经为错误处理所做的那样。

比如:

if let uploadData = self.imagePet?.jpegData(compressionQuality: 0.2) {

    storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
        if error != nil {
            print("error: \(String(describing: error))")
            return
        }

        // once you get here, the data is uploaded to Cloud Storage, and 
        // you can get the download URL

        storageRef.downloadURL(completion: {(url, error) in
            if error != nil {
                print(error!.localizedDescription)
                return
            }

            // once you get here, you have the download URL, so you can
            // write it to the database

            let data = [
                "pid" : newDocument.documentID,
                "urlImagePet" : storageRef.description,
                "image" : url!.absoluteString
            ]

            newDocument.setData(data) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }
            }

        })

    }


}

【讨论】:

  • 谢谢。你是对的,这对我来说非常有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 2020-07-29
  • 1970-01-01
  • 2022-12-19
相关资源
最近更新 更多