coil-kt / coil

Image loading for Android and Compose Multiplatform.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add real image size information to image result

rustamsmax opened this issue · comments

Sometimes we need real image size in order to draw extra decoration over an image

For example: I've multiple photos with label metadata. In order to draw labels at it's relative positions over an image I must know the the real image size

As a workaround I'm extending ImageLoader and setting decode factory to a custom one where I return custom drawable with real size info

ExecuteCallback is not supported , I used ExecuteCallback like this

painter = rememberImagePainter(
    data = localId ?: attachment.cloudUrl!!,
    builder = { size(OriginalSize) },
    onExecute = { previous, current ->
        val newHeight = with(density) { current.size.height.toDp() }
        if (leastHeightImage == 0.dp || leastHeightImage > newHeight) {
            leastHeightImage = newHeight
        }
        current.state == ImagePainter.State.Empty || previous?.request != current.request
    }
)

This is a row of images , I am getting the least image height from the row of images then setting that as height of every image because I want every image on that row to be equal to the height of least image height

This used to work but now I can't do this , Please help me achieve this with the newer version of coil compose which does not support onExecute !

I tried this layout as well , doesn't work

@Composable
private fun MinHeightRow(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    Layout(
        modifier = modifier,
        content = content
    ) { measurables, constraints ->
        // Don't constrain child views further, measure them with given constraints
        // List of measured children
        val placeables = measurables.map { measurable ->
            // Measure each children
            measurable.measure(constraints)
        }

        var minHeight = 0
        for (placeable in placeables) {
            if (minHeight == 0 || placeable.height < minHeight) {
                minHeight = placeable.height
            }
        }

        // Set the size of the layout as big as it can
       // I tried constraints that minHeight too , this layout didn't work
        layout(constraints.maxWidth, minHeight) {
            // Track the y co-ord we have placed children up to
            var xPosition = 0

            // Place children in the parent layout
            placeables.forEach { placeable ->
                // Position item on the screen
                placeable.placeRelative(x = xPosition, y = 0)

                // Record the y co-ord placed up to
                xPosition += placeable.width
            }
        }
    }
}

Hi @rustamsmax could you please share your code which can let me have the image size ! THanks in advance

Same needs as @rustamsmax , I thought about seveal ways to get the original size.

  • Use size(Size.ORIGINAL), the simplest but may cause OOM.
  • Reading from diskCache requires reading the entire file, which is a bit heavy.
  • Custom the Decode Result and other things, like @rustamsmax did, best solution but bad maintenance.

Can't believe this enhancement hasn't been added after almost a year.

Reading from diskCache requires reading the entire file, which is a bit heavy.

Reading the size or other metadata of a file does not require reading it entirely.