saket / telephoto

Building blocks for designing media experiences in Compose UI
https://saket.github.io/telephoto/
Apache License 2.0
869 stars 28 forks source link

Image blinks on opening #52

Closed kmnaseef closed 6 months ago

kmnaseef commented 8 months ago

Image blink once just after opened.

saket commented 8 months ago

Please add some more context in the form of a video or code to reproduce. It's hard to tell what's happening otherwise.

AleksandarIlic commented 7 months ago

It seems that .listener() is causing blinking. On my end it's blinking/flashing all the time.

val zoomSpec = ZoomSpec(maxZoomFactor = 4.0f)
val zoomableImageState = rememberZoomableImageState(rememberZoomableState(zoomSpec = zoomSpec))
ZoomableAsyncImage(
    modifier = modifier,
    state = zoomableImageState,
    model = ImageRequest.Builder(LocalContext.current)
        .data(imageUrl)
        .placeholderMemoryCacheKey(imageUrl)
        .crossfade(IMAGE_CROSSFADE_DURATION)
        .listener()
        .build(),
    contentDescription = null,
)

If I remove .listener() everything works fine.

Is there any other way to get notified when the image is loaded?

saket commented 7 months ago

From what I understand, this is happening because listener() is creating a new object on every recomposition, making telephoto think that the ImageRequest was changed. You'll need to remember your listener object (example).

I wonder how Coil is able to avoid this. Does AsyncImage not suffer from this problem?

AleksandarIlic commented 7 months ago

Thanks for the link.

I never had blinking problem with Coil. I'm using SubcomposeAsyncImage which also provides custom loading, error and success composable functions to render the states and it's really nice feature to have.

saket commented 7 months ago

Yep that's indeed nice to have. I want to explore if I can offer something similar in the future, but for now you're restricted to managing the loading & error states yourself.

kmnaseef commented 6 months ago

@saket , @AleksandarIlic thanks for the reply and sorry for late response. I am using ZoomableAsyncImage inside a HorizontalPager. When I add/remove images from the 'imageUriList', Image blinks. But Coil doesn't have this issue


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ImageViewerScreen(
    imageUriList: List<Uri>
){
    val context = LocalContext.current
    val state = rememberPagerState(initialPage = 0, 0f) { imageUriList.size }

    HorizontalPager(
        key = { pos ->
            imageUriList[pos]
        },
        state = state,
        modifier = Modifier.fillMaxSize()

    ){ page ->

        ZoomableAsyncImage(
            modifier = Modifier.fillMaxSize(),
            model = imageUriList[page],
            contentDescription = "",
            imageLoader = ImageLoader.Builder(context)
                .components {
                    add(ImageDecoderDecoder.Factory())
                    add(SvgDecoder.Factory())
                }
                .build()
        )
    }
}
saket commented 6 months ago

I wonder if it's because you're creating a new ImageLoader for each image. Does hoisting and remembering your loader change anything?

val context = LocalContext.current
val state = rememberPagerState(initialPage = 0, 0f) { imageUriList.size }
val imageLoader = remember {
    ImageLoader.Builder(context)
        .components {
            add(ImageDecoderDecoder.Factory())
            add(SvgDecoder.Factory())
        }
        .build()
}
HorizontalPager(
    key = { pos ->
        imageUriList[pos]
    },
    state = state,
    modifier = Modifier.fillMaxSize()
) { page ->
    ZoomableAsyncImage(
        modifier = Modifier.fillMaxSize(),
        model = imageUriList[page],
        contentDescription = "",
        imageLoader = imageLoader,
    )
}
kmnaseef commented 6 months ago

Thank you very much @saket, that was the issue. Now everything work perfect