【问题标题】:Download image with Kingfisher from Firebase Storage从 Firebase 存储使用 Kingfisher 下载图像
【发布时间】:2020-11-06 07:44:57
【问题描述】:

在我的应用中,我将图像存储在 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! 不能为空。

谁能帮帮我?

【问题讨论】:

    标签: ios swift google-cloud-firestore firebase-storage kingfisher


    【解决方案1】:

    将它移到成功块内

            case .success(_):
                print("success") 
               dataSourceArrayWithWishes[wishIDX].wishes.append(image: imageView.image!)
            case .failure(_):
                print("fail")
            }
        }
    }
    

    这个 imageView.kf.setImage(with: resource) { (result) in 是一个异步方法,这意味着dataSourceArrayWithWishes[wishIDX].wishes.append(image: imageView.image!) 崩溃的行在从firebase 获取图像之前运行,因此它为零

    【讨论】:

    • 好吧,有道理,感谢您的解释!
    猜你喜欢
    • 2016-12-28
    • 2017-01-16
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多