Referencing image in a folder in asset catalog

半城伤御伤魂 提交于 2019-12-20 17:47:21

问题


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.

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?


回答1:


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. You would add the following with proper formatting.

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



回答2:


Visual Instructions

(based on Xcode 10.1)

To have the path uniquely identify an asset catalog image, set the "Provides Namespace" attribute of the folder to true.

1) Select the asset catalog folder.

*The folder is yellow if "Provides Namespace" is false.

2) Make sure the Inspectors panel is visible



3) In the Inspectors panel, select the 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")


来源:https://stackoverflow.com/questions/24905796/referencing-image-in-a-folder-in-asset-catalog

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