简体   繁体   English

来自Firebase的缓存图片

[英]Cache Image from Firebase

I have multiple cells that currently hold different photos from Firebase. 我有多个单元格,目前从Firebase中保存不同的照片。 Every time a user loads these images then scrolls, they are re-downloaded which eats up data fast. 每次用户加载这些图像然后滚动时,它们都会被重新下载,这会快速吞噬数据。 I find this concerning to any user who has a metered data plan. 我发现这涉及任何拥有计量数据计划的用户。 What could I do to solve this? 我该怎么做才能解决这个问题? Does Firebase offer any options to cache downloaded images? Firebase是否提供缓存下载图像的任何选项?

This is how I am currently calling an image into a cell: 这就是我目前将图像调用到单元格中的方式:

if let imageName = post["image"] as? String {
        let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil {
            let image = UIImage(data: data!)
            cell.postImageView.image = image

Use Kingfisher to cache images. 使用Kingfisher缓存图像。 It's light and very easy to use. 它轻便且易于使用。 Just pass your url from firebase and it will automatically cache it. 只需从firebase传递你的网址,它就会自动缓存它。

let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)

Firebase already does cache the database locally, before it fetch real-time data from the server, so the problem is not as severe. Firebase在从服务器获取实时数据之前已经在本地缓存数据库,因此问题并不严重。 But if you want to do better caching, use Glide , Glide caches images and you can specify time signatures so it re-fetches images only if they are updated. 但是如果你想做更好的缓存,可以使用Glide ,Glide缓存图像,你可以指定时间签名,这样只有在更新图像时它才会重新获取图像。

This is super easy, but you do need to host your images in google cloud services, or aws, or anywhere even 这非常简单,但您确实需要在Google云服务或aws或其他任何地方托管您的图片

 ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
 Glide.with(this).load("url").into(imageView);

You might use Alamofire too. 你也可以使用Alamofire It does not handle caching automatically though, but against Kingfisher, it has the ability to handle almost all kinds of networking needs. 它不会自动处理缓存,但是对于Kingfisher,它能够处理几乎所有类型的网络需求。

PS: Yes I know that -generally- I do not need any networking capabilities if I'm using Firebase. PS:是的我知道 - 通常 - 如果我使用Firebase,我不需要任何网络功能。

But, for example; 但是,例如; since Firebase Database and Firestore cannot handle full-text search, you need to use third-party solutions, so, you might be in need of full-featured networking utility sometime. 由于Firebase数据库和Firestore无法处理全文搜索,因此您需要使用第三方解决方案,因此,您可能需要使用全功能网络实用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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