Jetpack Compose 线圈预载

发布于 2025-01-14 06:31:57 字数 2137 浏览 4 评论 0原文

我想将图像加载到启动屏幕上,以便我可以使用线圈将请求结果设置到其他屏幕上的背景,但我无法在线圈中完全实现这一点。如何将线圈请求的结果保存到 Cahce 并在其他屏幕上使用该结果?

启动画面

val imageLoader = ImageLoader.Builder(this)
        .memoryCache {
            MemoryCache.Builder(this)
                .maxSizePercent(0.25)
                .strongReferencesEnabled(true)
                .build()
        }
            
            
        .diskCache {
            DiskCache.Builder()
                .directory(this.cacheDir.resolve("image_cache"))
                .maxSizePercent(0.02)
                .build()
        }
        .build()

使用了这样的启动

    val context = LocalContext.current
    val request = ImageRequest.Builder(context)
        .memoryCacheKey(Constants.Cache.BACKGROUND_IMAGE_KEY)
        .data("https://www.example/image1.jpg")
        .target(
            onSuccess = {
                viewModel.skipImageRequest()
            },
            onError = {
                viewModel.skipImageRequest()
            }
        )
        .build()
    imageLoader.enqueue(request)

,我在另一个我想使用图像的屏幕上

@Composable
fun BackgroundImage(
    model: Any?,
    contentDescription: String? = "",
    modifier: Modifier = Modifier.fillMaxSize(),
    //placeholder: Painter? = painterResource(R.drawable.bg_placeholder),
    error: Painter? = painterResource(R.drawable.bg_placeholder),
    fallback: Painter? = painterResource(R.drawable.bg_placeholder),
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.FillBounds
) {
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data("https://www.example/image1.jpg")
            .placeholderMemoryCacheKey(Constants.Cache.BACKGROUND_IMAGE_KEY)
            .build(),
        contentDescription = contentDescription,
        modifier = modifier,
        //placeholder = placeholder,
        error = error,
        fallback = fallback,
        alignment = alignment,
        contentScale = contentScale,
    )
}

画面始终存在一个问题,占位符图像显示在页面初始化中我无法构造一个在加载图像时占位符不可见的结构并且直接从cahce读取和使用图像。

如果您支持我会很高兴

I want to load the image on the spash screen so that I can set the request result to the background on other screens using the coil, but I have not been able to fully realize this in the coil. How can I save the result of my coil request to Cahce and use this result on other screens?

Splash Screen

val imageLoader = ImageLoader.Builder(this)
        .memoryCache {
            MemoryCache.Builder(this)
                .maxSizePercent(0.25)
                .strongReferencesEnabled(true)
                .build()
        }
            
            
        .diskCache {
            DiskCache.Builder()
                .directory(this.cacheDir.resolve("image_cache"))
                .maxSizePercent(0.02)
                .build()
        }
        .build()

and I used like this on splash

    val context = LocalContext.current
    val request = ImageRequest.Builder(context)
        .memoryCacheKey(Constants.Cache.BACKGROUND_IMAGE_KEY)
        .data("https://www.example/image1.jpg")
        .target(
            onSuccess = {
                viewModel.skipImageRequest()
            },
            onError = {
                viewModel.skipImageRequest()
            }
        )
        .build()
    imageLoader.enqueue(request)

in another screen that I want to use image

@Composable
fun BackgroundImage(
    model: Any?,
    contentDescription: String? = "",
    modifier: Modifier = Modifier.fillMaxSize(),
    //placeholder: Painter? = painterResource(R.drawable.bg_placeholder),
    error: Painter? = painterResource(R.drawable.bg_placeholder),
    fallback: Painter? = painterResource(R.drawable.bg_placeholder),
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.FillBounds
) {
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data("https://www.example/image1.jpg")
            .placeholderMemoryCacheKey(Constants.Cache.BACKGROUND_IMAGE_KEY)
            .build(),
        contentDescription = contentDescription,
        modifier = modifier,
        //placeholder = placeholder,
        error = error,
        fallback = fallback,
        alignment = alignment,
        contentScale = contentScale,
    )
}

There is an always problem the placeholder image is shown in initialize of page I could not construct a structure where the placeholder is not visible while the image is being loaded and the image is read and used directly from the cahce.

I would be very happy if you support

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

智商已欠费 2025-01-21 06:31:57

Coil在1.x版本内部使用了OkHttp的DiskCache
对于 2.x,他们引入了磁盘缓存来存储您的结果。

有关更多信息,请阅读

Coil internally uses OkHttp's DiskCache in 1.x version
For 2.x they have introduced a disk cache to store your result.

For further information read this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文