0

我在桌面版 kotlin compose 中加载图像时遇到问题

它给出了以下错误:

找不到 image.png 资源

我在项目的 src 文件夹中有文件

我不确定问题是否出在代码或我导入图像的方式上,或者即使问题是 kotlin compose for desktop 仍处于实验性质

val imageModifier = Modifier
            .height(240.dp)
            .fillMaxWidth()
            .clip(RoundedCornerShape(12.dp))

        Image(bitmap = useResource("image.png") { loadImageBitmap(it) },
            "image",
            imageModifier,
            contentScale = ContentScale.Fit)
4

1 回答 1

2

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

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

于 2021-09-20T03:13:24.617 回答