xcode 资产目录:访问不同文件夹中同名的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33284412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:05:23  来源:igfitidea点击:

Asset Catalog: Access images with same name in different folders

iosobjective-cxcodebundleassets

提问by Alexey Pelekh

There is an images.xcassetsin my project, it contains an iconsfolder and two subfolders (firstFolder, secondFolder) with images. Both of my subfolders have the same number of icons and the same icons names(for different themes of my app).

images.xcassets我的项目中有一个,它包含一个icons文件夹和两个带有图像的子文件夹 ( firstFolder, secondFolder)。我的两个子文件夹都有相同数量的图标和相同的图标名称(适用于我的应用程序的不同主题)。

So what I'm looking for: I need to get the needed icon(for current theme) from my bundle.

所以我在寻找什么:我需要从我的包中获取所需的图标(对于当前主题)。

I've tried to do something like this:

我试图做这样的事情:

NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString *imageName = [bundle.bundlePath stringByAppendingPathComponent:@"icons/firstFolder/neededIcon"];

It does not work.

这是行不通的。

回答by joern

Click on each folder in the assets catalog and select Provides Namespacein the Utilities View:

单击资产目录中的每个文件夹,然后Provides Namespace在实用程序视图中选择:

enter image description here

在此处输入图片说明

You will see that the folder then becomes blue and you can see the path to the image above the images.

您将看到该文件夹​​随后变为蓝色,并且您可以在图像上方看到图像的路径。

You can then access the image like this:

然后,您可以像这样访问图像:

imageView.image = UIImage(named: "folder1/Image")

or in Objective-C:

或在 Objective-C 中:

imageView.image = [UIImage imageNamed:@"folder1/Image"];