为不同尺寸的 ImageView 再次下载毕加索图像?
Picasso Image downloaded again for an ImageView with different dimensions?
我在我的应用程序中使用 Picasso 来有效地加载图像,并且正在做他在 best.The 问题上的工作,目前如果我用相同的 [=26= 多次调用 Picasso.with()
],但是每次使用具有不同尺寸的图像视图时,都会下载图像 again.In Glide
我们有方法 .diskCacheStrategy()
和 DiskCacheStrategy.SOURCE
来解决 problem.is毕加索还有其他方法吗?
在 this 查询中,我们将获得 Glid 的解决方案,但不是 Picasso.how 我可以重复使用图像而无需重新下载不同尺寸的 ImageView。
这是我正在使用的代码
Picasso.with(context)
.load(URI)
.placeholder(R.drawable.ic_profile_thumb)
.resize(180, 180).centerInside()
.into(viewHolder.imgThumbnail);
您可以使用来自同一供应商 "SquareUp" 的 OkHttp
。
This answer 详细告诉你怎么做。
Picasso 不会将图像缓存到磁盘,Picasso 只有内存缓存。磁盘缓存由网络库(OKhttp)完成。所以写入磁盘缓存的图像永远不会重新调整大小。
如果多次下载相同的 url 图片,您可能需要检查您的网络库。
如果您的项目正在使用 okhttp 库,那么 picasso 会自动将其用作默认下载器。如果您正在使用任何其他网络库,那么您将必须编写自己的下载程序并使用
进行设置
Picasso.Builder(context).downloader(customDownloader)
this link 可能会让事情变得清晰
更新: link 很旧,顺便说一句,这是最新版本的变化:
https://github.com/square/picasso/blob/master/CHANGELOG.md
正如@Sotti 所说,Picasso
有 Automatic memory and disk caching
他们在介绍部分提到了这一点:(最后一部分)**
http://square.github.io/picasso/#introduction
In this query we will get the solution for Glid but not for
Picasso.how can i reuse the image without redownloading for different
dimensions ImageView
.
实际上,Picasso
只有一个 内存缓存 ,但您可能想为此使用 OkHttp
。
检查这个 link: 其中说:
If you are referring about the disk cache, then no Picasso does not
support that at the moment. This is by design because the disk layer
cache is done by the HTTP layer
and makes no distinction about it.
也看看这个:https://corner.squareup.com/2013/05/picasso-one-dot-oh.html
Picasso automatically utilizes a memory and disk cache (provided by
the HTTP client
) to speed up bitmap loading. For development you can
enable the display of a colored marker which indicates the image
source.
另外,检查这个问题:How to implement my own disk cache with picasso library - Android?
使用OkHttp
将文件保存在自定义缓存目录中:
OkHttpClient okHttpClient = new OkHttpClient();
File customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyCache");
okHttpClient.setCache(new Cache(customCacheDirectory, Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(mainActivity).downloader(okHttpDownloader).build();
picasso.load(imageURL).into(viewHolder.image);
发件人:
如您所见,使用 setCache
。
毕加索默认不调整图像大小。如果您像下一个一样进行简单的调用...
Picasso.with(context).load("https://goo.gl/v9EkbF").into(imageView);
...Picasso 正在缓存全尺寸图像。从那一刻起,每次您调用相同的 URL,Picasso 都会重新使用该全尺寸图像并让 GPU 在视图中调整大小。
使用 resize() 方法您只缓存调整大小的图像,如果您使用不同的尺寸,Picasso 将需要重新下载图像。
不要使用 resize() 选项,你会得到更好的带宽使用,但要小心内存使用。
Picasso 中的默认值
Picasso 确实有自动内存和磁盘缓存。最近请求的任何图像都将在内存缓存中。如果不存在,Picasso 将检查磁盘缓存。如果它在磁盘上不可用,只有 Picasso 才会启动网络请求。
所有请求的图像都存储在两个缓存中,直到必须删除它们才能释放 space。
2.5.2 中的默认值是:
- 可用应用程序 RAM 的 15% 的 LRU 内存缓存
- 2% 存储的磁盘缓存 space 最多 50MB 但不少于 5MB。
- Picasso 将使用 OkHttp 作为 Http 客户端,如果它作为 Gradle 依赖包含在内的话。否则 Picasso 将回退到 HttpUrlConnection。
您可以像这样增加磁盘缓存(例如 100MB):
Picasso picasso = new Picasso
.Builder(this)
.downloader(new OkHttpDownloader(getCacheDir(), 100000000))
.build();
Picasso.setSingletonInstance(picasso);
更改磁盘缓存大小不会更改缓存策略。
内存策略
- MemoryPolicy.NO_CACHE: 图像不会从内存中提供。这不会避免从磁盘提供图像。为了避免磁盘看不起 Network Policies.
- MemoryPolicy.NO_STORE: 图像不会存储在内存中。将此用于只加载一次的图像。
例子
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
网络策略
- NetworkPolicy.NO_CACHE: 图像不会从磁盘缓存中提供。
- NetworkPolicy.OFFLINE:图像将(如果可能)仅从缓存(内存或磁盘)提供,而不是从网络提供。
示例:
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageView);
内存和缓存注意事项
- Glide 默认使用 RGB565 并将调整大小的图像缓存到
内存。
- Picasso 默认使用 RGB888 并将全尺寸图像缓存到
内存并让 GPU 在绘制时实时调整大小。
RGB565 是 RGB888 的一半大小。使用 RGB565 和较小图像的结果是 Picasso 的内存占用比 Glide 的更高(可能是 3x/4x)。
这些只是默认值。 Glide 可以配置为使用 RGB888,Picasso 可以配置为使用 RGB565。 Picasso 可以配置为仅将调整大小的图像放入内存,如 Glide。
相同设置下,内存占用几乎相同
带宽注意事项
- Glide 为每个尺寸缓存单独的文件。如果您加载其他尺寸的
同样的图片,之前会重新下载,调整大小,然后
已缓存。
- Picasso 总是从缓存中获取全尺寸图像,然后让 GPU 在视图中调整大小。
您可以要求 Glide 缓存所有内容,全尺寸图像和调整大小的图像,但默认情况下 Glide 的带宽消耗更高。
备注
Picasso 2.6 即将到来,我不知道什么是 different/new。
来源
我在我的应用程序中使用 Picasso 来有效地加载图像,并且正在做他在 best.The 问题上的工作,目前如果我用相同的 [=26= 多次调用 Picasso.with()
],但是每次使用具有不同尺寸的图像视图时,都会下载图像 again.In Glide
我们有方法 .diskCacheStrategy()
和 DiskCacheStrategy.SOURCE
来解决 problem.is毕加索还有其他方法吗?
在 this 查询中,我们将获得 Glid 的解决方案,但不是 Picasso.how 我可以重复使用图像而无需重新下载不同尺寸的 ImageView。
这是我正在使用的代码
Picasso.with(context)
.load(URI)
.placeholder(R.drawable.ic_profile_thumb)
.resize(180, 180).centerInside()
.into(viewHolder.imgThumbnail);
您可以使用来自同一供应商 "SquareUp" 的 OkHttp
。
This answer 详细告诉你怎么做。
Picasso 不会将图像缓存到磁盘,Picasso 只有内存缓存。磁盘缓存由网络库(OKhttp)完成。所以写入磁盘缓存的图像永远不会重新调整大小。
如果多次下载相同的 url 图片,您可能需要检查您的网络库。
如果您的项目正在使用 okhttp 库,那么 picasso 会自动将其用作默认下载器。如果您正在使用任何其他网络库,那么您将必须编写自己的下载程序并使用
进行设置Picasso.Builder(context).downloader(customDownloader)
this link 可能会让事情变得清晰
更新: link 很旧,顺便说一句,这是最新版本的变化:
https://github.com/square/picasso/blob/master/CHANGELOG.md
正如@Sotti 所说,Picasso
有 Automatic memory and disk caching
他们在介绍部分提到了这一点:(最后一部分)**
http://square.github.io/picasso/#introduction
In this query we will get the solution for Glid but not for Picasso.how can i reuse the image without redownloading for different dimensions
ImageView
.
实际上,Picasso
只有一个 内存缓存 ,但您可能想为此使用 OkHttp
。
检查这个 link: 其中说:
If you are referring about the disk cache, then no Picasso does not support that at the moment. This is by design because the disk layer cache is done by the
HTTP layer
and makes no distinction about it.
也看看这个:https://corner.squareup.com/2013/05/picasso-one-dot-oh.html
Picasso automatically utilizes a memory and disk cache (provided by the
HTTP client
) to speed up bitmap loading. For development you can enable the display of a colored marker which indicates the image source.
另外,检查这个问题:How to implement my own disk cache with picasso library - Android?
使用OkHttp
将文件保存在自定义缓存目录中:
OkHttpClient okHttpClient = new OkHttpClient();
File customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyCache");
okHttpClient.setCache(new Cache(customCacheDirectory, Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(mainActivity).downloader(okHttpDownloader).build();
picasso.load(imageURL).into(viewHolder.image);
发件人:
如您所见,使用 setCache
。
毕加索默认不调整图像大小。如果您像下一个一样进行简单的调用...
Picasso.with(context).load("https://goo.gl/v9EkbF").into(imageView);
...Picasso 正在缓存全尺寸图像。从那一刻起,每次您调用相同的 URL,Picasso 都会重新使用该全尺寸图像并让 GPU 在视图中调整大小。
使用 resize() 方法您只缓存调整大小的图像,如果您使用不同的尺寸,Picasso 将需要重新下载图像。
不要使用 resize() 选项,你会得到更好的带宽使用,但要小心内存使用。
Picasso 中的默认值
Picasso 确实有自动内存和磁盘缓存。最近请求的任何图像都将在内存缓存中。如果不存在,Picasso 将检查磁盘缓存。如果它在磁盘上不可用,只有 Picasso 才会启动网络请求。
所有请求的图像都存储在两个缓存中,直到必须删除它们才能释放 space。
2.5.2 中的默认值是:
- 可用应用程序 RAM 的 15% 的 LRU 内存缓存
- 2% 存储的磁盘缓存 space 最多 50MB 但不少于 5MB。
- Picasso 将使用 OkHttp 作为 Http 客户端,如果它作为 Gradle 依赖包含在内的话。否则 Picasso 将回退到 HttpUrlConnection。
您可以像这样增加磁盘缓存(例如 100MB):
Picasso picasso = new Picasso
.Builder(this)
.downloader(new OkHttpDownloader(getCacheDir(), 100000000))
.build();
Picasso.setSingletonInstance(picasso);
更改磁盘缓存大小不会更改缓存策略。
内存策略
- MemoryPolicy.NO_CACHE: 图像不会从内存中提供。这不会避免从磁盘提供图像。为了避免磁盘看不起 Network Policies.
- MemoryPolicy.NO_STORE: 图像不会存储在内存中。将此用于只加载一次的图像。
例子
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
网络策略
- NetworkPolicy.NO_CACHE: 图像不会从磁盘缓存中提供。
- NetworkPolicy.OFFLINE:图像将(如果可能)仅从缓存(内存或磁盘)提供,而不是从网络提供。
示例:
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageView);
内存和缓存注意事项
- Glide 默认使用 RGB565 并将调整大小的图像缓存到 内存。
- Picasso 默认使用 RGB888 并将全尺寸图像缓存到 内存并让 GPU 在绘制时实时调整大小。
RGB565 是 RGB888 的一半大小。使用 RGB565 和较小图像的结果是 Picasso 的内存占用比 Glide 的更高(可能是 3x/4x)。
这些只是默认值。 Glide 可以配置为使用 RGB888,Picasso 可以配置为使用 RGB565。 Picasso 可以配置为仅将调整大小的图像放入内存,如 Glide。
相同设置下,内存占用几乎相同
带宽注意事项
- Glide 为每个尺寸缓存单独的文件。如果您加载其他尺寸的 同样的图片,之前会重新下载,调整大小,然后 已缓存。
- Picasso 总是从缓存中获取全尺寸图像,然后让 GPU 在视图中调整大小。
您可以要求 Glide 缓存所有内容,全尺寸图像和调整大小的图像,但默认情况下 Glide 的带宽消耗更高。
备注
Picasso 2.6 即将到来,我不知道什么是 different/new。
来源