毕加索将图像加载到目标中 [英] Picasso Load Image into Target
问题描述
我正在使用毕加索下载各种图像.通常,我只是将它们显示在ImageView
中,但是在这种情况下,我想牢牢引用它们,以便可以在不同的地方使用它们,而不必参考缓存或重新下载它们.这是我尝试执行的操作(请注意,该课程还有更多内容-我将其范围缩小到与该问题相关的部分):
public class MapLayer {
private Context mContext;
private String mType;
private Drawable mIcon = null;
public MapLayer (Context context, String type) {
mContext = context;
mType = type;
downloadIcon();
}
public Drawable getIcon() {return mIcon;}
private void downloadIcon() {
String url = mContext.getString(R.string.maps_icon_url).replace("${type}", mType));
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "on bitmap loaded");
mIcon = new BitmapDrawable(mContext.getResources(), bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d(TAG, "on bitmap failed");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d(TAG, "on prepare load");
mIcon = placeHolderDrawable;
}
};
ImageDownloader.getSharedInstance().load(url).into(target);
}
}
在每种情况下,我都会得到输出:
on prepare load
但是没有别的.我的图标始终为空.我从其他称为getIcon()
的类中知道这一点.
我在这里想念什么?感谢您的帮助.
Picasso持有Target
实例的引用很弱,因此您的Target
似乎被垃圾回收了.
参见: https://github.com/square/picasso/issues/352 >
最好将Target
保留为实例字段.
public class MapLayer {
...
private Target target;
private void downloadIcon() {
...
target = new Target() {
...
};
ImageDownloader.getSharedInstance().load(url).into(target);
}
}
I'm using Picasso to download various images. Usually, I just display these in an ImageView
but in this situation, I want to hold a strong reference to them so that I can use them in different places without having to refer back to the cache or re-download them. Here is how I am attempting to do that (note that there is more to this class - I've just narrowed it down to the parts that are relevant to this question):
public class MapLayer {
private Context mContext;
private String mType;
private Drawable mIcon = null;
public MapLayer (Context context, String type) {
mContext = context;
mType = type;
downloadIcon();
}
public Drawable getIcon() {return mIcon;}
private void downloadIcon() {
String url = mContext.getString(R.string.maps_icon_url).replace("${type}", mType));
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "on bitmap loaded");
mIcon = new BitmapDrawable(mContext.getResources(), bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d(TAG, "on bitmap failed");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d(TAG, "on prepare load");
mIcon = placeHolderDrawable;
}
};
ImageDownloader.getSharedInstance().load(url).into(target);
}
}
In every case, I get the output:
on prepare load
but nothing else. My icon is always null. I know this from other classes where I call getIcon()
.
What am I missing here? Thanks for any help.
Picasso holds Target
instance with a weak reference, So your Target
seems to be garbage collected.
see: https://github.com/square/picasso/issues/352
It is better to hold Target
as an instance field.
public class MapLayer {
...
private Target target;
private void downloadIcon() {
...
target = new Target() {
...
};
ImageDownloader.getSharedInstance().load(url).into(target);
}
}
这篇关于毕加索将图像加载到目标中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!