尝试显示数据库中的图片时出现致命错误

Fatal error when trying to display picture from database

所以我试图显示用户选择并上传到 Firebase 数据库的图像。我有这段代码可以显示他们的用户名、标签和个人资料图片。这是我收到错误的地方的屏幕截图。我已经查看了有关此错误的其他表格,但已尝试应用它并仍然遇到相同的问题。感谢您提出如何提前解决此问题的建议!

[

嗯,看来 data 很可能是 nil,所以 UIImage() 没有被创建。您可以对其进行测试并抛出错误,如果合适的话:

enum DataError: Error {
    case noData
}

guard let data = NSData(contentsOf: NSURL(string: databaseProfilePic)!) else {
    print("Could not create valid NSData from databaseProfilePic")
    throw DataError.noData
}

如果您不喜欢 guard,那么您可以考虑分配 if

if let data = NSData(contentsOf: NSURL(string: databaseProfilePic)!) {
    self.setProfilePic(imageView:self.imageView1, imageToSet:UIImage(data: data as Data)!)
}

我会考虑检查 URL databaseProfilePic 以及获得 URL 的结果。

尝试像

一样使用 if let 解包 databaseprofilePic 的值
if let profilePic = dict!["photoURL"] as? String {

  //your code to set profile pic

}