本文介绍了无法从 SwiftUI 框架中的资产目录访问的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含单个 SwiftUI View 的 iOS 框架.然后我添加了一个带有单个图像的资产目录.尝试从 SwiftUI 视图访问该图像没有任何作用 - 我看到一个空白图像.

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")

Xcode Preview 没有显示任何内容,视图也没有显示我编写的使用该框架的测试目标的任何内容.

The Xcode Preview doesn't show anything, nor does the view show anything for a test target I wrote that uses the framework.

所以问题是 - 如何将图像与使用 SwiftUI 的框架捆绑在一起?这不支持吗?我可以让它工作的唯一方法是将图像移动到测试目标的资产目录.不幸的是,这意味着我无法在开发框架时预览我正在处理的设计.

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.

推荐答案

默认

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

即.在应用程序包中.

您必须明确指定资产目录所在的包(框架),如

You have to explicitly specify bundle (of framework) in which Assets catalog is located, as

Image("MyImage", bundle: bundle)

其中 bundle 通过类或标识符以通常的方式实例化.

where bundle is instantiated in usual way by class or identifier.

这篇关于无法从 SwiftUI 框架中的资产目录访问的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:47