如何在SwiftUI iPad Playground中引用图像文件?

问题描述 投票:1回答:1

我已将相册中的图像(jpg)添加到UserResources文件夹中(通过Playground应用程序右上角的+菜单)。声明SwiftUI映像时如何引用该文件?

我尝试使用整个路径:

let path: String = { Bundle.main.paths(forResourcesOfType: "JPG", inDirectory: nil)[0] }()

似乎为我提供了所需文件“ PICT0024.JPG”的路径:

/var/mobile/Containers/Data/PluginKitPlugin/F01FA67E-97CA-4590-915A-C8071507C6E0/Library/Application Support/Main Module Resources Bundles/55929C8C-46C9-4691-B86B-389D1473E9A8.bundle/Contents/Resources/PICT0024.JPG

然后我声明:

Image(systemName: path)

但是未显示图像。我可能有错误的参考,但无法弄清楚我需要指定的内容。

哦,就是这个

Image(“PICT0024.JPG”) 

也不起作用。

引用该图像文件的正确方法是什么?

请限制对SwiftUI 5的回答并在iPad上进行编辑。目前无法使用XCode。

谢谢。

image ipad swiftui swift-playground
1个回答
0
投票

Image(systemName: path)

此构造函数用于系统SF符号

Image(“PICT0024.JPG”)

此构造函数用于资产目录中的图像

您必须像这样使用的外部图像

Image(uiImage: UIImage(contentsOfFile: path))

© www.soinside.com 2019 - 2024. All rights reserved.