本文介绍了适用于Android的本地图像缓存解决方案:Square Picasso,Universal Image Loader,Glide,Fresco?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Android中的异步图像加载和缓存库.我本打算使用Picasso,但是我发现Universal Image Loader在GitHub上更受欢迎.有人知道这两个库吗?优点和缺点的摘要将是很好的.

I am looking for an asynchronous image loading and caching library in Android. I was going to use Picasso, but I found Universal Image Loader is more popular on GitHub. Does anyone know about these two libraries? A summary of pros and cons would be great.

(我所有的图像都在本地磁盘上,因此我不需要联网,因此我认为Volley不适合)

(All my images are on disk locally, so I don't need networking, therefore I don't think Volley is a fit)

推荐答案

2018年9月更新:几年后,我需要几乎相同的东西作为本地图像缓存解决方案.这次,UIL尚未积极开发.我比较了流行的库,结论很简单:只需使用Glide.它更加强大和可配置.几年前,我不得不分叉并对UIL进行更改. Glide在缓存策略和使用自定义键的多级分辨率缓存方面都支持我的所有用例.只需使用Glide!

Update Sep 2018: After several years, I needed the almost same thing for a local image caching solution. This time around, UIL has not been in active development. I compared the popular libraries, and the conclusion is pretty no-brainer: just use Glide. It's much more powerful and configurable. Years ago I had to fork and make changes to UIL. Glide supports all my use cases in terms of caching strategy and multiple levels of resolution caching with custom keys. Just use Glide!

Koushik Dutta的比较主要是针对速度基准.他的帖子仅涉及非常基本的内容,并不针对本地图像.问了问题之后,我想与我分享Picasso和UIL的经验.毕加索和UIL均可加载本地图像.我最初尝试使用Picasso感到很高兴,但是后来我决定改用UIL以获得更多自定义选项.

Koushik Dutta's comparison is mostly for speed benchmark. His post only touched very basic things, and is not specific for local images. I'd like to share my experiences with Picasso and UIL after I asked the question. Both Picasso and UIL can load local images. I first tried Picasso and was happy, but later I decided to switch to UIL for more customization options.

毕加索:

  • 毕加索的流利界面很好.但是,通过"with","into","load"跳来跳去,您实际上不知道背后是什么.令人困惑的是返回的内容.

  • Picasso's fluent interface is nice. But jumping around with "with", "into", "load" you actually don't know what's behind the scene. It's confusing what's returned.

Picasso允许您指定确切的目标大小.当您遇到内存不足或性能问题时,此功能很有用,您可以在某些图像质量与速度之间进行权衡.

Picasso allows you to specify exact target size. It's useful when you have memory pressure or performance issues, you can trade off some image quality for speed.

图像的键值已按大小缓存,当您显示不同大小的图像时,此功能很有用.

Images are cached with size in its key, it's useful when you display images with different sizes.

您可以自定义内存缓存大小.但是它的磁盘缓存仅用于http请求.对于本地图像,如果您关心加载速度,最好有一个缩略图磁盘缓存,这样您就不必每次都读取一张图像的几MB.毕加索没有此机制来调整缩略图大小并将其保存在磁盘上.

You can customize the memory cache size. But its disc cache is only for http requests. For local images, if you care about loading speed, it's good to have a thumbnail disk cache so you don't have to read several MBs for an image every time. Picasso does not have this mechanism resizing and saving thumbnails on disk.

Picasso不会公开对其缓存实例的访问. (当您首次配置Picasso并保留它时,您可以保留它.)

Picasso does not expose the access to its cache instance. (You can get a hold of it when you first configure Picasso and keep it around...).

有时您想异步将图像读入侦听器返回的位图.令人惊讶的是,毕加索没有. "fetch()"不会传递任何东西. "get()"用于同步读取,"load()"用于异步绘制视图.

Sometimes you want to asynchronously read image into a bitmap returned by a listener. Surprisingly Picasso doesn't have that. "fetch()" dose not pass back anything. "get()" is for synchronously read, and "load()" is for asynchronously draw a view.

毕加索的主页上只有几个简单的示例,您必须通读无序的Javadoc以获得高级用法.

Picasso only has a few simple examples on the homepage, and you'll have to read through the unordered javadoc for advanced usages.

UIL:

  • UIL使用构建器进行自定义.几乎所有内容都可以配置.

  • UIL uses builders for customization. Almost everything can be configured.

UIL不允许您指定要加载到视图中的大小.它根据视图的大小使用一些规则.它不如毕加索灵活.我无法加载较低分辨率的图像以减少内存占用. (可以通过在源代码中添加ImageSize参数并绕过视图大小检查来轻松修改此行为)

UIL does not allow you to specify the size you want to load into a view. It uses some rules based on the size of the view. It's not as flexible as Picasso. I have no way to load a lower resolution image to reduce memory footprint. ( this behavior can be easily modified by adding an ImageSize argument in in the source code and bypass the view size checking)

UIL提供了可自定义的磁盘缓存,您可以使用它来缓存具有指定大小的缩略图.但这并不完美.这是详细信息. (如果您关心速度,并且希望像我的情况一样需要多级缩略图缓存,则可以修改源代码,让磁盘缓存使用"memoryKey",并使它也对大小敏感)

UIL provides customizable disc cache, you can use this to cache the thumbnails with specified size. But it's not perfect. Here are the details. ( if you care about speed and want multiple levels of thumbnail caching, like my case, you can modify the source code, let the disk cache use "memoryKey", and make it also size sensitive)

UIL默认情况下将不同大小的图像缓存在内存中,并且可以在配置中将其关闭.

UIL by default caches images of different sizes in memory, and it can be turned off in configuration.

UIL公开了您可以访问的后备内存和磁盘缓存.

UIL exposes the backing memory and disk cache you can access.

UIL提供了获取位图或加载到视图的灵活方法.

UIL provides flexible ways you can get a bitmap or load to a view.

UIL在文档方面更好. UIL在Github页面上提供了详细的用法,并且有一个链接的教程.

UIL is better in documentation. UIL gives the detailed usages on the Github page, and there's a linked tutorial.

我建议从毕加索开始,如果您需要更多控制和自定义,请使用UIL.

I suggest starting with Picasso, if you need more control and customization, go for UIL.

这篇关于适用于Android的本地图像缓存解决方案:Square Picasso,Universal Image Loader,Glide,Fresco?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!