资产目录:访问不同文件夹中具有相同名称的图像 [英] Asset Catalog: Access images with same name in different folders
问题描述
我的项目中有一个 images.xcassets
,它包含一个图标
文件夹和两个子文件夹(<$带图像的c $ c> firstFolder , secondFolder
)。我的两个子文件夹都有相同数量的图标和相同的图标名称(对于我的应用程序的不同主题)。
There is an images.xcassets
in my project, it contains an icons
folder 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).
所以我正在寻找:
我需要从我的包中获取所需的图标(对于当前主题)。
So what I'm looking for: I need to get the needed icon(for current theme) from my bundle.
我试过这样的事情:
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString *imageName = [bundle.bundlePath stringByAppendingPathComponent:@"icons/firstFolder/neededIcon"];
它不起作用。
推荐答案
单击资产目录中的每个文件夹,然后在Utilities视图中选择提供命名空间
:
Click on each folder in the assets catalog and select Provides Namespace
in the Utilities View:
您将看到该文件夹变为蓝色,您可以看到上面图像的路径图片。
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")
或在Objective-C中:
or in Objective-C:
imageView.image = [UIImage imageNamed:@"folder1/Image"];
这篇关于资产目录:访问不同文件夹中具有相同名称的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!