简体   繁体   English

从 Firebase 存储下载带有翠鸟的图像

[英]Download image with Kingfisher from Firebase Storage

Inside my app I store images in Firebase Storage and also its url to Cloud Firestore .在我的应用程序中,我将图像存储在Firebase 存储中,并将其url存储到Cloud Firestore Now when I try to retrieve this image I am using Kingfisher but I think I have a misunderstanding of how Kingfisher works.现在,当我尝试检索此图像时,我正在使用Kingfisher ,但我认为我对Kingfisher的工作方式有误解。 This is my code:这是我的代码:

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!)

What I would like to to here is if the imageUrl is correct, retrieve it and then save it to dataSourceArrayWithWishes as a UIImage .我想在这里如果imageUrl是正确的,检索它,然后将其作为UIImage保存到dataSourceArrayWithWishes If not, then just save an empty UIImage .如果没有,那么只需保存一个空的UIImage

However when a valid imageUrl is given, it fails because imageView.image!但是,当给出有效的imageUrl时,它会失败,因为imageView.image! must not be null.不得为 null。

Can anyone help me out here?有谁可以帮我离开这里吗?

Move it here inside success block将其移至成功块内

        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 hence it's nil 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM