【问题标题】:Problems Loading image in Kotlin Compose desktop在 Kotlin Compose 桌面中加载图像时出现问题
【发布时间】:2021-09-19 22:54:39
【问题描述】:

我在桌面版 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)

【问题讨论】:

    标签: kotlin android-jetpack-compose compose-desktop


    【解决方案1】:

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

    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-02-20
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      相关资源
      最近更新 更多