swift - 尝试显示数据库中的图片时出现致命错误
问题描述
所以我试图显示用户选择并上传到 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)!)
}
我会查看 URLdatabaseProfilePic
以及获取该 URL 的结果。
推荐阅读
- java - 当我有子查询时,通过在 java 中使用 prepare 语句在运行时向查询添加条件
- python - 使用 sklearn learn 对列表列表进行矢量化?
- python - 我写了一个简单的石头剪刀布程序,但我想在三个中做到最好,不知道如何将它写到我现有的代码中
- .htaccess - 如何将包含静态和动态变量的 URL 重定向到动态 URL
- laravel - 如何向 laravel-passport 访问令牌添加自定义声明?
- graphite - Graphite django-admin.py raise ImproperlyConfigured("SECRET_KEY 设置不能为空或没有名为 'graphite' 的模块
- java - 首次启动时将活动设为默认活动
- apache-kafka - 具有单个 Kafka 消费者的负载平衡节点 js 应用程序
- javascript - 如何在 Angular Universal 中降低服务器端渲染的成本
- python - 如何提取两个角色之间的兴趣值?