Images inaccessible from asset catalog in a SwiftUI framework

天涯浪子 提交于 2020-12-07 04:54:36

问题


I've created an iOS framework that contains a single 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.

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.

So the question is - how do I bundle images with a framework that uses 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.


回答1:


By default

Image("MyImage") is Image("MyImage", bundle: Bundle.main)

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.



来源:https://stackoverflow.com/questions/61459814/images-inaccessible-from-asset-catalog-in-a-swiftui-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!