swift - Swift UIImageView Firebase DispatchQueue
问题描述
我正在使用 firebase 来保存和加载我的图像。我在 Xcode 中创建了一个新视图,并且正在使用与加载配置文件图像相同的代码。然而,这现在抛出一个错误,说 url 字符串是 nil。图片 url 数据在“DispatchQueue.global().async”之后消失。这可能是什么原因造成的,我该如何跟踪?很奇怪,这段代码如何适用于其他视图,但对于这个新视图,它会引发错误。
let businessProfilePicture = dictionary["profPicString"] as! String
if businessProfilePicture.count > 0 {
let url = URL(string: businessProfilePicture)
print(url)
print("printing the url here to check")
DispatchQueue.global().async {
let dataURL = try? Data(contentsOf: url!)
print(dataURL)
print("printing the data url here")
DispatchQueue.main.async {
print(dataURL)
print("Printing Data to check")
let image = UIImage(data: dataURL!)?.potter_circleo
self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
self.businessProfilePicture.image = image
}
}
完整代码
func getWorkLocation() {
let uid = Auth.auth().currentUser?.uid
var profPicURL: String = ""
Database.database().reference().child("employees").child(uid!).child("Business").observe(.value, with: { snapshot in
if snapshot.exists() {
let dictionary = snapshot.value as? NSDictionary
self.businessName.text = dictionary?["businessName"] as? String
self.businessStreet.text = dictionary?["businessStreet"] as? String
self.businessCity.text = dictionary?["businessCity"] as? String
profPicURL = dictionary?["profPicString"] as! String
// set image
if profPicURL.count > 0 {
let url = URL(string: profPicURL)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
let image = UIImage(data: data!)?.potter_circle
self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
self.businessProfilePicture.image = image
}
}
} else {
let image = UIImage(named: "profile picture")?.potter_circle
self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
self.businessProfilePicture.image = image
}
} else {
self.businessName.text = ""
self.businessStreet.text = "Go to Add Work Location to send request"
self.businessCity.text = ""
self.deleteButton.isEnabled = false
}
})
}
解决方案
您确定您创建的 URLprofPicURL
是正确创建的吗?
URL(string:)
可以失败并返回nil
。如果您继续隐式打开它,Data(contentsOf: url!)
您将崩溃。
同样,try? Data(contentsOf: url)
可以返回 nil。如果是这样,那么当您隐式打开它时,UIImage(data: data!)
您将崩溃。
正如 Jacob 在评论中所说,您需要了解有关隐式展开的选项的更多信息。为了让你开始,你可能会像这样构建你的代码:
if let url = URL(string: profPicURL) {
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url),
let image = UIImage(data: data)?.potter_circle
{
DispatchQueue.main.async {
self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
self.businessProfilePicture.image = image
}
} else {
// raise an an error or set self.businessProfilePicture.image to a generic image or something
}
}
} else {
// raise an an error or set self.businessProfilePicture.image to a generic image or something
}
推荐阅读
- angular - 错误类型错误:无法读取 null Angular/Firebase 的属性“标题”
- angular - 使用 karma jasmine 使用路由器 navigateByUrl 对功能进行单元测试
- java - 如何使用 maven-project 插件分别读取多模块 maven 项目信息(如依赖项)
- intellij-idea - Sass map-get 的自动补全
- node.js - 从 Node.js 中的 JSON 对象中检索特定级别级别
- python - Jupyter notebook 内核与 conda 环境不同
- azure-devops - 在 Azure DevOps 管道中运行 NUnit 测试
- azure - 如何重试从 Azure 事件网格向逻辑应用发送事件
- r - R中GGplots的for循环中的数学符号
- python - 气流安装问题 SyntaxError:调用“打印”时缺少括号