glide icon indicating copy to clipboard operation
glide copied to clipboard

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

Open mradlmaier opened this issue 2 years ago • 1 comments

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!

mradlmaier avatar Nov 07 '23 10:11 mradlmaier

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.

riktheveggie avatar Jul 05 '24 02:07 riktheveggie