在Kotlin撰写桌面加载图像的问题



在kotlin compose for desktop中加载图像时遇到问题

给出如下错误:

image.png资源未找到

我有项目src文件夹下的文件

我不确定问题出在代码上还是我导入图片的方式上,或者问题出在kotlin排版的实验性

val imageModifier = Modifier
.height(240.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
Image(bitmap = useResource("image.png") { loadImageBitmap(it) },
"image",
imageModifier,
contentScale = ContentScale.Fit)

将您的图像文件保存在资源文件夹,然后像这样使用

val imageModifier = Modifier
.height(240.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
Image(painter = painterResource("image.png"),
contentDescription = "image",
imageModifier,
contentScale = ContentScale.Fit
)

painterResource支持栅格(BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP)和矢量格式(SVG, XML矢量绘制)。

更多信息请访问https://github.com/JetBrains/compose-jb/tree/master/tutorials/Image_And_Icons_Manipulations

最新更新