【问题标题】:Firebase storage image by username用户名的 Firebase 存储图像
【发布时间】:2017-08-06 17:54:02
【问题描述】:

我已按用户名将我的用户个人资料图片保存在 Firebase 存储中。

如何为用户检索图像?

我不断收到相同的错误消息:

在展开可选值时意外发现 nil

这是我的代码:

let filePath = "\("profileImage/\(self.userNameText.text!)")"
        self.storageRef.child(filePath).data(withMaxSize: INT64_MAX, completion: { (data, error) in
            let profileImage = UIImage(data: data!)
            self.profileImage.image = profileImage
            })

【问题讨论】:

    标签: swift image firebase firebase-storage


    【解决方案1】:

    unexpectedly found nil while unwrapping an Optional value 表示您正在尝试使用返回 nil 的值,因此它不存在。

    您在哪一行收到此错误?

    【讨论】:

    • 我在 let profileImage = UIImage(data:data) 行中遇到错误
    • 是的,所以这个错误意味着你强制解开参数“数据”,但数据是空的,这会导致崩溃。所以你的问题出在这一行:let filePath = "\("profileImage/\(self.userNameText.text!)")" self.storageRef.child(filePath).data(withMaxSize: INT64_MAX, completion: { (data, error) in
    【解决方案2】:

    FirebaseStorage 似乎找不到您的图片。我已经为您调整了一个示例,以便您可以阅读您将收到的错误消息。

    let filePath = String(format:"profileImage/%@", self.userNameText.text!)
    
    // Create a reference to the file you want to download
    let islandRef = storageRef.child(filePath)
    
    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    islandRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in
      if let error = error {
        // Uh-oh, an error occurred!
        print(error.localizedDescription)
      } else {
        // Data for "images/island.jpg" is returned
        let image = UIImage(data: data!)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2023-03-13
      相关资源
      最近更新 更多