用毕加索下载大量图像

编程入门 行业动态 更新时间:2024-10-27 15:21:43
毕加索下载大量图像 - 内存不足(Downloading a lot of images with Picasso - Out of memory)

我目前正在使用Picasso下载这样的图像:

ImageView imageView = (ImageView) rootView.findViewById(R.id.imagePreview); imageView.setAdjustViewBounds(true); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); Transformation transformation = new Transformation() { @Override public Bitmap transform(Bitmap source) { int targetHeight = 500; double aspectRatio = (double) source.getWidth() / (double) source.getHeight(); int targetWidth = (int) (targetHeight * aspectRatio); Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false); if (result != source) { // Same bitmap is returned if sizes are the same source.recycle(); } return result; } @Override public String key() { return "transformation" + " desiredWidth"; } }; RequestCreator requestCreator; Picasso picasso = Picasso.with(context); requestCreator = picasso .load(resource) .transform(transformation) .skipMemoryCache() .error(R.drawable.ic_placeholder_ricerca);

我甚至正在调整图像大小,但仍然不够。 问题是我有一个无穷无尽的listView,所以我一直在下载图像,之后内存会变满。 有任何想法如何防止这个问题? 我想扩大memoryCache(在代码中我也试图完全跳过但没有结果),但我不知道如何做到这一点(代码示例在线不起作用:()。

I'm currently using Picasso to download of images like that:

ImageView imageView = (ImageView) rootView.findViewById(R.id.imagePreview); imageView.setAdjustViewBounds(true); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); Transformation transformation = new Transformation() { @Override public Bitmap transform(Bitmap source) { int targetHeight = 500; double aspectRatio = (double) source.getWidth() / (double) source.getHeight(); int targetWidth = (int) (targetHeight * aspectRatio); Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false); if (result != source) { // Same bitmap is returned if sizes are the same source.recycle(); } return result; } @Override public String key() { return "transformation" + " desiredWidth"; } }; RequestCreator requestCreator; Picasso picasso = Picasso.with(context); requestCreator = picasso .load(resource) .transform(transformation) .skipMemoryCache() .error(R.drawable.ic_placeholder_ricerca);

I'm even resizing the images but it's still not enough. The problem is that I have an endless listView, so I keep downloading images and after a will the memory get full. Any idea of how to prevent the problem? I thought to enlarge the memoryCache (in the code I have also tried to skip completely but without results), but I have no idea of how to do that (the code example online don't work :( ).

最满意答案

问题在于我正在缓存自己想要更快地制作东西的图像,而不是因为毕加索自己处理这个问题而无缘无故地挤了堆。 所以,只是删除了优化,我们走了。

The problem was that I was caching myself the images thinking to make stuff faster, instead I was crowding the heap for no reason since Picasso handles itself this problem. So, just erased the optimizations and here we go.

更多推荐

本文发布于:2023-07-26 19:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1280248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:毕加索   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!