简体   繁体   English

用户名的Firebase存储映像

[英]Firebase storage image by username

I have saved my users profile images in Firebase Storage by username. 我已按用户名将用户个人资料图像保存在Firebase存储中。

How do I retrieve the image for a user? 如何为用户检索图像?

I keep getting the same error message: 我不断收到相同的错误消息:

unexpectedly found nil while unwrapping an Optional value 展开Optional值时意外发现nil

Here is my code: 这是我的代码:

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
            })

unexpectedly found nil while unwrapping an Optional value means that you're trying to use a value that's returning nil so it doesn't exist. unexpectedly found nil while unwrapping an Optional value表示您正在尝试使用返回nil的值,因此该值不存在。

On what exact line are you getting this error? 您在哪条直线上遇到此错误?

It seems like FirebaseStorage couldn't find your image. 似乎FirebaseStorage无法找到您的映像。 I've adjusted an example for you so you can read the error message you'll get. 我为您调整了一个示例,以便您阅读将收到的错误消息。

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!)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM