I'm using Coil to load an image into a layout with...
# compose
l
I'm using Coil to load an image into a layout with dynamic height:
Copy code
Column {
            Image(
                painter = rememberImagePainter(backdropImageUrl),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxWidth(),
            )
    }
However it doesn't render anything. The only way to make it work is like this:
Copy code
Column {
            Image(
                painter = rememberImagePainter(backdropImageUrl) {
                    size(OriginalSize)
                },
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxWidth(),
            )
    }
Is it correct? Can't Coil just automatically predict the size of the image based on the width (and calculate the height from it based on the aspect ratio)? It is really a super basic use case, would be nice to have some sensible default behavior for it.
Ok, looks like it's being worked on: https://github.com/coil-kt/coil/issues/953
i
Sounds like
AsyncImage
is exactly what you need (since the limitation is specifically with how a
painter
doesn't get measured at all in these cases)
🙏 1
🔥 1
c
I don't know if I'm being helpful at all but the compose samples include this "sizing interceptor" that basically wait to request an image from the network until there is a size for the image. https://github.com/android/compose-samples/blob/e1d757b5352709da16986720f336e3120b[…]ndroidx/compose/samples/crane/util/UnsplashSizingInterceptor.kt maybe you'll find it handy. /shrug
l
Oh yes, this looks very interesting indeed. Currently I have a simpler use case (the downloaded image is always fixed width), but for the future it could be useful to be able to download differently sized images based on the device pixel resolution 👍
c
Yeah. We use a service like unsplash to resize images based on what the actual image size calls for and so the above was helpful in implementing that.
👍 1
551 Views