如何从 Firebase 下载图像到 Swift,然后更快地在 UIImageView 中显示它?

How can I download an image from Firebase to Swift and then display it in a UIImageView more quickly?

我正在从 Firebase 下载图像到 Swift(大多数大约 5 MB),然后在 UIImageView 中显示它们。每个只出现一次,因此无需缓存它们供以后查看。大多数时候,他们需要一两秒钟来加载。有没有办法缩短这个时间?

我的代码:

Database.database().reference().child("movies").child(movieNumString).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
    let UrlString: String? = ((dictionary["posterPhotoURL"] as? String))
    let Url = URL(string: (UrlString)!)

    self.moviePosterImageView.sd_setImage(with: Url, placeholderImage: #imageLiteral(resourceName: "PImage"), options: [], completed: { (image, error, cacheType, url) in
            self.moviePosterImageView.image = image
    })
}

以后请参阅堆栈溢出 guidelines

最重要的是,您应该担心图像的数据和大小,下载它们所需的时间会随着您减小尺寸而减少。尝试将大小减小到几 kb。

在 Firebase 中,您可以拥有运行服务器端的功能,例如,您可以调整图像的大小。请参阅 here for all the function examples and for resizing this 是使用 sharp 包的示例。