简体   繁体   English

如何获取资产目录组中的所有图像名称?

[英]How can I get all image names in asset catalog group?

In my app I had a folder of images from which I was getting the names to use as keys to a Dictionary. 在我的应用程序中,我有一个图像文件夹,我从中获取名称作为词典的键。 I have now moved all images to an asset catalog and created groups for the relevant folders. 我现在已将所有图像移动到资产目录并为相关文件夹创建组。 The code using FileManager is not working anymore as it can not find any folders (due to the assets being compiled into a .car file). 使用FileManager的代码不再工作,因为它找不到任何文件夹(由于资产被编译成.car文件)。

How should I approach this? 我该怎么做呢?

So (following matt's suggestion) here is what I did: 所以(按照亚特的建议)这就是我所做的:

I created a RunScript build phase to run before compiling 我创建了一个RunScript构建阶段,在编译之前运行

在此输入图像描述

and used a script to create a txt file with the names of the files that I can then use during runtime 并使用脚本创建一个txt文件,其中包含我可以在运行时使用的文件名

#!/bin/sh
>./your_app_folder/fileNames.txt
for FILE in ./your_app_folder/Images.xcassets/actions/*; do
echo $FILE >> ./your_app_folder/fileNames.txt
done

You have two choices: 你有两个选择:

  • Don't Do That. 不要那样做。 Go right on using a folder of images. 继续使用图像文件夹。

  • Supply the keys in some other way. 以其他方式提供密钥。 If you don't want to hard-code them into the code itself, you could make a text file. 如果您不想将它们硬编码到代码本身中,则可以创建一个文本文件。 But of course you will have to keep that text file updated as you add / remove images. 但是,当您添加/删除图像时,您必须更新该文本文件。

You'll notice that I didn't say you could magically introspect the asset catalog. 你会注意到我没有说你可以神奇地反省资产目录。 That's because you can't. 那是因为你做不到。

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

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