如何在 Compose UI Android Jetpack 中将 url 加载到 Image 中? [英] How do I load url into Image into DrawImage in Compose UI Android Jetpack?
问题描述
我可以用val image = +imageResource(R.drawable.header)
并使用 DrawImage(image)
从 Drawable Resource 加载图像,
I can use
val image = +imageResource(R.drawable.header)
and use DrawImage(image)
to load image from Drawable Resource,
但是如何将字符串 url 加载到 DrawImage(image)
中?我试过使用 Glide,但它需要加载到 imageView 中.同时 DrawImage(image)
不接受 imageView 的输入.
But how do I load a string url into the DrawImage(image)
?. I've tried using Glide, but it needs to load into imageView. meanwhile DrawImage(image)
doesn't take input from imageView.
谢谢.
推荐答案
盯着 1.0.x
实现它的最佳方式是使用 Coil-Compose 库.
Staring with 1.0.x
the best way to achieve it is to use the Coil-Compose library.
在你的 build.gradle
中添加依赖
dependencies {
implementation("io.coil-kt:coil-compose:1.3.1")
}
然后只需使用:
Image(
painter = rememberImagePainter("your url"),
contentDescription = "My content description",
)
这会加载通过 rememberImagePainter
传入的 url
,然后使用标准的 Image
组合显示生成的图像.
This loads the url
passed in with rememberImagePainter
, and then displays the resulting image using the standard Image
composable.
这篇关于如何在 Compose UI Android Jetpack 中将 url 加载到 Image 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!