简体   繁体   English

引用资产目录中文件夹中的图像

[英]Referencing image in a folder in asset catalog

In an asset catalog you can create folders. 在资产目录中,您可以创建文件夹。

Inside the folders all the image names have to be unique. 在文件夹内,所有图像名称必须是唯一的。

If you have an image called myImage and you try to name another image myImage then it will automatically get renamed to myImage-1 . 如果你有一个名为图像myImage并尝试名称的另一个图像myImage然后它会自动被重新命名为myImage-1

But if you have two folders then you can have images with the same name in different folders. 但是,如果您有两个文件夹,那么您可以在不同的文件夹中使用相同名称的图像。

So, there must be a way to reference these? 那么,必须有一种方法可以参考这些吗?

Does anyone know how to reference an image by the folder and image name inside the asset catalog? 有谁知道如何通过资产目录中的文件夹和图像名称引用图像?

I spent a ton of time but finally figured this out. 我花了很多时间,但终于弄明白了。 Answered here . 这里回答 Copied below. 复制如下。

Select each folder in your asset catalog that you'd like to add a namespace for. 选择资产目录中要为其添加命名空间的每个文件夹。 Go to the folder attributes and select under the name "Provides Namespace". 转到文件夹属性,然后选择名称“提供命名空间”。 This should properly set sub-directories for your final asset catalog. 这应该正确设置最终资产目录的子目录。

You can do this manually by editing each folder's Contents.json. 您可以通过编辑每个文件夹的Contents.json手动完成此操作。 You would add the following with proper formatting. 您可以使用正确的格式添加以下内容。

"properties" : {
      "provides-namespace" : true
}

Visual Instructions 视觉指令

(based on Xcode 10.1) (基于Xcode 10.1)

To have the path uniquely identify an asset catalog image, set the "Provides Namespace" attribute of the folder to true . 要使路径唯一标识资产目录图像,请将文件夹的“提供命名空间”属性设置为true

1) Select the asset catalog folder. 1)选择资产目录文件夹。

在此输入图像描述

*The folder is yellow if "Provides Namespace" is false . *如果“提供命名空间”为false则文件夹为黄色。

2) Make sure the Inspectors panel is visible 2)确保Inspectors面板可见

在此输入图像描述

3) In the Inspectors panel, select the Attributes Inspector. 3)在Inspectors面板中,选择Attributes Inspector。 Check the "Provides Namespace" checkbox. 选中“提供命名空间”复选框。

在此输入图像描述

Notice that the folder icon turns blue indicating that "Provides Namespace" is true. 请注意,文件夹图标变为蓝色,表示“提供命名空间”为真。

在此输入图像描述

When referencing the image in code, remember to include the namespace. 在代码中引用图像时,请记住包含命名空间。

let image = UIImage(named: "Icon/Menu")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM