[英]Images inaccessible from asset catalog in a SwiftUI framework
I've created an iOS framework that contains a single SwiftUI View
.我创建了一个 iOS 框架,其中包含一个 SwiftUI
View
。 I've then added an Asset Catalog with a single image.然后我添加了一个带有单个图像的资产目录。 Trying to access that image from the SwiftUI view doesn't do anything - I see a blank image.
尝试从 SwiftUI 视图访问该图像没有任何作用 - 我看到一个空白图像。
Image("MyImage")
The Xcode Preview doesn't show anything, nor does the view show anything for a test target I wrote that uses the framework. Xcode 预览版没有显示任何内容,视图也没有显示我使用该框架编写的测试目标的任何内容。
So the question is - how do I bundle images with a framework that uses SwiftUI?所以问题是 - 我如何将图像与使用 SwiftUI 的框架捆绑在一起? Is this not supported?
这不支持吗? The only way I could get this to work was to move the image to the test target's asset catalog instead.
我可以让它工作的唯一方法是将图像移动到测试目标的资产目录中。 Unfortunately this means that I cannot preview the designs I'm working on when developing the framework.
不幸的是,这意味着我无法在开发框架时预览我正在处理的设计。
By default默认
Image("MyImage")
is Image("MyImage", bundle: Bundle.main)
Image("MyImage")
是Image("MyImage", bundle: Bundle.main)
ie. IE。 in bundle of application.
在应用程序包中。
You have to explicitly specify bundle (of framework) in which Assets catalog is located, as您必须明确指定资产目录所在的(框架的)捆绑包,如
Image("MyImage", bundle: bundle)
where bundle
is instantiated in usual way by class or identifier.其中
bundle
由 class 或标识符以通常的方式实例化。
Yes, you can load a custom image or custom SF Symbol within a Swift Package.是的,您可以在 Swift Package 中加载自定义图像或自定义 SF 符号。
The SwiftUI invocation is: SwiftUI 调用是:
Image(“my-symbol-name“, bundle: .module)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.