[英]Download image with Kingfisher from Firebase Storage
在我的应用程序中,我将图像存储在Firebase 存储中,并将其url
存储到Cloud Firestore 。 现在,当我尝试检索此图像时,我正在使用Kingfisher ,但我认为我对Kingfisher的工作方式有误解。 这是我的代码:
let imageView = UIImageView()
imageView.image = UIImage()
if let imageUrl = URL(string: imageUrlString) {
let resource = ImageResource(downloadURL: imageUrl)
imageView.kf.setImage(with: resource) { (result) in
switch result {
case .success(_):
print("success")
case .failure(_):
print("fail")
}
}
}
dataSourceArrayWithWishes[wishIDX].wishes.append(image: imageView.image!)
我想在这里如果imageUrl
是正确的,检索它,然后将其作为UIImage
保存到dataSourceArrayWithWishes
。 如果没有,那么只需保存一个空的UIImage
。
但是,当给出有效的imageUrl
时,它会失败,因为imageView.image!
不得为 null。
有谁可以帮我离开这里吗?
将其移至成功块内
case .success(_):
print("success")
dataSourceArrayWithWishes[wishIDX].wishes.append(image: imageView.image!)
case .failure(_):
print("fail")
}
}
}
This imageView.kf.setImage(with: resource) { (result) in
is an asynchnous method meaning that the line being crashed dataSourceArrayWithWishes[wishIDX].wishes.append(image: imageView.image!)
runs before the image is fetched from firebase因此它为零
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.