Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add real image size information to image result #1213

Open
rsktash opened this issue Mar 23, 2022 · 5 comments
Open

Add real image size information to image result #1213

rsktash opened this issue Mar 23, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@rsktash
Copy link

rsktash commented Mar 23, 2022

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

@rsktash rsktash added the enhancement New feature or request label Mar 23, 2022
@rsktash
Copy link
Author

rsktash commented Mar 23, 2022

image

@wakaztahir
Copy link

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
            }
        }
    }
}

@wakaztahir
Copy link

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

@oOJohn6Oo
Copy link

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.

@saket
Copy link

saket commented Apr 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants