bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.64k stars 6.12k forks source link

Is this the correct way to load images with Glide in Jetpack Compose? (Memory leak???) #5327

Open mradlmaier opened 11 months ago

mradlmaier commented 11 months ago

I loading images into a LazyColumn, and while the full-size image is loading, a much smaller thumbnail image shall and be replaced by the full-sized image.

Based on the documentation, my code looks like this:

@Composable 
fun GlideImagePage(name: String, description: String, imageRes: Int, imageUrl: String, thumbnailUrl : String) {
val context = LocalContext.current
GlideImage(
    model = imageUrl,
    contentDescription = description,
    modifier = Modifier
        .padding(1.dp),
    contentScale = ContentScale.FillWidth
){
    Glide.with(context).load(imageUrl).thumbnail(
        Glide.with(context).load(thumbnailUrl)
    )
}
}

This code seems to be working, but i fear that there will be 3 requests created, one for the thumbnail image, one for the full-size image by the code in the inner braces, and before that, another request for the full-size image in the GlideImage Tag, Is this the case and if so, what happens to the full-size image request? Will it leak?

Can somebody enlighten me?

Thanks!

riktheveggie commented 3 months ago

Not sure if you're still looking for an answer to this, but I started playing with Glide today and had the same question.

The docs say to call it.thumbnail passing in a requestManager - but how do we get one when we don't have a fragment? It turns out you had the answer above - Glide.with(context).

So tweaking your example, I have the following:

@Composable 
fun GlideImagePage(name: String, description: String, imageRes: Int, imageUrl: String, thumbnailUrl : String) {
  val context = LocalContext.current
  GlideImage(
    model = imageUrl,
    contentDescription = description,
    modifier = Modifier
        .padding(1.dp),
    contentScale = ContentScale.FillWidth
  ){
    it.thumbnail(Glide.with(context).asDrawable().load(thumbnailUrl))
  }
}

This seems to work for me, I tested by using https://sample-videos.com/img/Sample-jpg-image-50kb.jpg and https://sample-videos.com/img/Sample-jpg-image-30mb.jpg.