[英]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.