毕加索和上下文 [英] Picasso and context

查看:87
本文介绍了毕加索和上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直将Picasso用作图像加载器库.我将其与Dagger和OkHtttp结合使用.

I have been playing lately with Picasso as an image loader library. I use it in conjunction with Dagger and OkHtttp.

关于此库的我唯一的问题是上下文的使用以及通过构建器对库的实例化.

My only questions regarding this library are the usage of context and the instantiation of the library through the builder.

我不完全确定需要什么上下文(找不到有关它的文档),以及我们应该使用哪个上下文(ApplicationContext或ActivityContext?),以及为什么.

I am not entirely sure what it is the context needed (can't find documentation about it) and also which context should we use (ApplicationContext or ActivityContext?) and why.

Jake Wharton的示例(举个例子,很好地结合了所有示例)在u2020中,只有一个毕加索实例,它是在适当的应用程序上下文下创建的.像这样:

Looking at examples (btw great example app to see all this in combination) u2020 by Jake Wharton, only a single instance of Picasso it is created with the application context in place. Something like this:

@Provides
    @Singleton
    Picasso providePicasso(@ApplicationContext Context context, OkHttpClient client) {
        Picasso picasso = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(client))
                .listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception e) {
                        Log.e("Picasso", "Failed to load image:" + uri);
                    }
                })
                .build();

        return picasso;
    } 

这用作全局对象,仅实例化一次.我的问题是为什么不在活动"级别上实例化一个新的毕加索实例(使用配置LRUCache的相同全局全局OkHttpClient并事先注入)并将活动作为上下文传递?今天上午,我在Github Picasso线程中阅读了应该使用Application Context的内容,但没有提供更多详细信息.

This is use as a global object and only instantiated once. My question is why not instantiating a new picasso instance on Activity level (with the same global OkHttpClient which configures the LRUCache and it is injected previously) and passing the activity as context? I was reading this morning in a Github Picasso thread that Application Context should be used but didn't give more details about it.

作为结论,我的问题是: -使用的上下文是什么,我们应该使用哪个上下文. -为什么要使用全局对象而不是活动级别实例.

So as conclusion, my question are: - What is the context used for and which one should we use. - Why using a global object and not an Activity level instance.

谢谢!

推荐答案

选择哪个都无所谓,当使用默认的Picasso.with(Context)方法或Builder时,它将从以下位置检索应用程序Context给定的Context:

It doesn't matter which you choose, when using the default Picasso.with(Context) method, or the Builder, it will retrieve the application Context from the given Context:

/** Start building a new {@link Picasso} instance. */
public Builder(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context must not be null.");
  }
  this.context = context.getApplicationContext();
}

存根复制自毕加索.java#Builder .

如果您实际上想在每个活动中创建一个 new 实例: 对于您创建的每个Picasso实例,基本上都将创建一个新的缓存:第一个实例中缓存的图像不会在第二个实例中重复使用.您很可能在这里遇到OutOfMemoryException,因为毕加索无法处理此问题.

If you actually want to create a new instance in each activity: For each instance of Picasso you create, you basically create a new cache: images cached in the first instance will not be reused in the second instance. You are very likely to run into OutOfMemoryExceptions here, as Picasso does not handle this.

这篇关于毕加索和上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆