【问题标题】:Fatal error when trying to display picture from database尝试显示数据库中的图片时出现致命错误
【发布时间】:2020-04-23 16:08:03
【问题描述】:

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

[

【问题讨论】:

  • 在swift中使用DataURL代替NSDataNSURL

标签: swift xcode fatal-error


【解决方案1】:

好吧,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 的结果。

【讨论】:

    【解决方案2】:

    尝试使用if let 解开databaseprofilePic 的值

    if let profilePic = dict!["photoURL"] as? String {
    
      //your code to set profile pic
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      相关资源
      最近更新 更多