What's the best way to display images from URLs? I...
# compose
r
What's the best way to display images from URLs? I've got some code that's about 6 months old, that uses Picasso. I need to rewrite it because it's displaying "loading image" every time I scroll away and back.
r
Use coil
🙏 2
c
If you're doing multiplatform then maybe https://github.com/alialbaali/Kamel
r
It's a plain Android app. I'm trying to use it with an Accompanist HorizontalPager, so images fill the screen width. So far it's just creating a thin line, no image visible: Code:
Copy code
HorizontalPager(state = pagerState, Modifier.fillMaxWidth()) { page ->
     if (page >= 0 && page < listing.images.size) {
         Image(painter = rememberImagePainter(listing.images[page]),
               contentDescription = null,
               contentScale = ContentScale.FillWidth,
               modifier = Modifier.fillMaxWidth()
         )
c
Try setting a set height and width. Like size(100.dp) If that works then it just means that you need to set a height.
r
Yes, that works, but I want it to match screen width, and height can be whatever, just keeping the aspect ratio.
c
Set a width and use aspect ratio modifier
FillMaxWidth().aspect ratio(16/9f)
r
But I want it to use the aspect ratio of the image.
c
You won't know aspect ratio of the image until it's downloaded. And if you do that then your layouts will assuredly jump.
r
I might prefer that to having a mushed image.
c
Don't have a mushed image. Just set a crop type of center
Content scale = crop
r
Thank you. I went with
Fit
, to not loose edges of the image, but same idea. Maybe I can figure out how to adjust it based on the loaded image, but this will work for now.
c
Based on image will result in layout shift. https://twitter.com/addyosmani/status/1334029534123868160?s=19
You can use a set aspect ratio if your images from the web are using a set aspect ratio too.
r
In my case I'm loading a lot of images of products provided by 3rd parties. I don't know the exact dimensions or aspect ratios beforehand.
c
Exactly why a set aspect ratio on the client side with a center crop is the way to go.