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

时间:2023-01-14 13:56:50

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?

我该怎么做呢?

2 个解决方案

#1


17  

So (following matt's suggestion) here is what I did:

所以(按照马特的建议)我所做的是:

I created a RunScript build phase to run before compiling

我创建了一个运行脚本构建阶段,在编译之前运行它

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

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

#2


7  

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.

您会注意到,我并没有说您可以神奇地内省资产目录。那是因为你不能。

#1


17  

So (following matt's suggestion) here is what I did:

所以(按照马特的建议)我所做的是:

I created a RunScript build phase to run before compiling

我创建了一个运行脚本构建阶段,在编译之前运行它

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

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

#2


7  

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.

您会注意到,我并没有说您可以神奇地内省资产目录。那是因为你不能。