如何将图像上传到Firebase存储器

问题描述 投票:0回答:1

当我将图像上传到Firebase存储时,用户照片显示为空白。我曾尝试使用Firebase指南,但它们使我感到困惑,如何重组此代码以解决此错误?

获取图像的下载URL错误

let storage = Storage.storage()
                   let storageRef = storage.reference()
                   let imagesRef = storageRef.child("profileImage.jpg")
            let localFile = URL(string: "appspot.com")!
                  let metadata = StorageMetadata()
                   let uploadTask = imagesRef.putFile(from: localFile, metadata: metadata)
                       guard metadata != nil else {
                           print("Image Upload Metadata Error")
                           print(error.debugDescription)
                           return
                       }


                       imagesRef.downloadURL { (url, error) in
                           guard let downloadURL = url else {
                               print("Error getting downloadURL of image")
                               return
                           }


                       }
ios swift firebase firebase-storage
1个回答
0
投票

您多次定义文件/引用路径。您只需要做一次。如果文件显示为空白,请确保文件本身没有问题。以下代码是您所需要的。

        let storage = Storage.storage()
        let storageRef = storage.reference()
        let imagesRef = storageRef.child("profileImage.jpg")

        let uploadTask = imagesRef.putFile(from: localFile, metadata: metadata){
            guard metadata != nil else {
                print("Image Upload Metadata Error")
                print(error.debugDescription)
                return
            }

            //If metadata returned, upload was successful
            //Get the image downloadURL and return it via callback
            imageRef.downloadURL { (url, error) in
                guard let downloadURL = url else {
                    print("Error getting downloadURL of image")
                    return
                }


            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.