首页 > 解决方案 > 尝试显示数据库中的图片时出现致命错误

问题描述

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

[错误代码截图[1] setProfilePic 函数

标签: swiftxcodefatal-error

解决方案


好吧,这似乎很可能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)!)
}

我会查看 URLdatabaseProfilePic以及获取该 URL 的结果。


推荐阅读