Zxxxxx
Zxxxxx

Reputation: 417

Big images loading with Picasso

I have a list view where I load image with person name. There are around 538 items in the list view. I get the images of persons from a url. Currently I am doing the following:-

Picasso.with(getContext()).load(url).resize(70,70).onlyScaleDown().into(im1);

The images present in the URL are very large in size and loading of some images takes a lot of time and it eats up my memory also while loading. Can some one please help me in loading the images efficiently and fastly.

Example image for one person can be found in the following URL:-

https://theunitedstates.io/images/congress/original/D000626.jpg

Upvotes: 5

Views: 9353

Answers (2)

Hitesh Dhamshaniya
Hitesh Dhamshaniya

Reputation: 2182

As per the new version of Picasso

Picasso.get().load(galleryList[position].mediaUrl).fit()
                .placeholder(R.mipmap.ic_launcher).into(holder?.ivImags)

Upvotes: 0

Luiz Fernando Salvaterra
Luiz Fernando Salvaterra

Reputation: 4182

You can use onlyScaleDown() to resize

Picasso  
    .with(context)
    .load(imageUrl)
    .resize(6000, 2000)
    .onlyScaleDown() // the image will only be resized if it's bigger than 6000x2000 pixels.
    .into(imageViewResizeScaleDown);

Or you can use fit()

Picasso  
    .with(context)
    .load(imageUrl)
    .fit()
    // call .centerInside() or .centerCrop() to avoid a stretched image
    .into(imageViewFit);

fit() is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView. There are two things to know about fit(). First, calling fit() can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit() with an ImageView as the target (we'll look at other targets later).

The advantage is that the image is at the lowest possible resolution, without affecting its quality. A lower resolution means less data to be hold in the cache. This can significantly reduce the impact of images in the memory footprint of your app. In summary, if you prefer a lower memory impact over a little faster loading times, fit() is a great tool.

Upvotes: 13

Related Questions