【问题标题】:Firebase image object does not exist?Firebase 图像对象不存在?
【发布时间】:2018-06-11 19:25:02
【问题描述】:

我目前正在学习如何向 Firebase 上传/下载图片。但是,我遇到了两个问题:

  1. 当我将图像上传到 Firebase 控制台时,它说它的类型是“application/octet-stream”,这不是我想要的(我上传了一个 jpeg 文件)

代码如下:

@IBAction func completeSignUpButton(_ sender: UIButton) {
    let userProfileImageRef = storage.child("userProfileImage")

    //UPloading the photo into firebase
    let data = Data()
    //uploading user profile picture
    if self.userInitialPhoto.image != nil {
        if let uploadData = UIImagePNGRepresentation(self.userInitialPhoto.image!) {
            userProfileImageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil {
                    print(error?.localizedDescription)
                    return
                } else {
                    print("Image upload success!")
                    //self.dismiss(animated: true, completion: nil)
                    self.performSegue(withIdentifier: "completeRegistrationSegue", sender: self)
                }
            })

        }

    } else {
        print("You need to pick a picture!")

    }

}

所以这可能是导致我的第二个问题的问题:

  1. 当我尝试将图像从 Firebase 加载到我的应用程序中的 UIImage 时,它​​显示 Object Optional(\"userID\")/userProfileImage 不存在。"

这是我的代码:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    docRef = Firestore.firestore().collection("user Status").document("Current Status")
    self.userStatusTableView.reloadData()
    let referencePath = Storage.storage().reference(withPath: "\(userID)/userProfileImage")
    let userProfileImageRef = Storage.storage().reference().child("\(userID)/userProfileImage")
    userProfileImageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
        if error != nil {
            print("There's an eror downloading the image\(error?.localizedDescription)")
        } else {
            self.userProfileImage.image = UIImage(data: data!)
        }
    }
}

我想知道我应该如何解决这些问题。

提前感谢您的帮助,祝大家新年快乐。

【问题讨论】:

    标签: ios firebase uiimage firebase-storage


    【解决方案1】:

    在将图像上传到 firebase 时,您将元数据传递为 nil。你需要在那里传递图像元数据

    let metaData = FIRStorageMetadata()
    metaData.contentType = "image/jpg"
    

    在上传 complitionBlock 中,您可以使用获取该图片的 URL

    if error == nil , let meta = meta {
         if let url = meta.downloadURL()?.absoluteString {
               //use this url to access your image
         }
    }
    

    【讨论】:

    • 谢谢我现在已经上传了 jpeg 但是我仍然无法下载图像。当我尝试将网址打印出来时,它给了我零。你知道如何解决这个问题吗?虽然图片 url 确实存在于我的 firebase 控制台中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2020-02-12
    • 2019-12-05
    • 2018-12-22
    • 2020-09-13
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多