获取资源文件夹中子目录的路径 [英] Get Path to Subdirectory in Resources Folder

查看:27
本文介绍了获取资源文件夹中子目录的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在资源文件夹的子目录中获取所有 png 文件的数组.在我的应用程序资源文件夹中,我创建了一个名为图像"的文件夹.该文件夹包含我需要显示的所有 png 文件.

i'm trying to get an array of all png files in a subdirectory of the Resources Folder. In my apps Resources Folder I have created a Folder named "images". This folder holds all the png-files I need to display.

这是我尝试获取路径的方式:

This is the way I tried to get the Path:

    NSString *imagepath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"images/"];

NSLog(@"Path to Images: %@", imagepath);

NSArray * paths = [NSBundle pathsForResourcesOfType: @"png" inDirectory:imagepath];
NSMutableArray * allImageNames = [[NSMutableArray alloc] init];

for ( NSString * path in paths )
{
    if ( [[path lastPathComponent] hasPrefix: @"AQ"] )
        continue;

    [allImageNames addObject: [path lastPathComponent]];
}

这样我就得到了一个像.../appname.app/images这样的路径

This way I get a Path like …/appname.app/images

但是如果我尝试这样做,数组总是空的.我做错了什么?

But if i try to do it that way, the array is always empty. What am I doing wrong?

问候,查拉克

推荐答案

刚刚解决了.

NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png"
 inDirectory:@"random pictures"];

 NSLog(@"pngs in my dir:%@", pngs);

目录结构:资源/随机图片".

dir structure: "Resources/random pictures".

这可行,但是,当您将文件添加到资源"时,您需要选中为任何添加的文件夹创建文件夹引用",而不是为任何添加的文件夹创建组".

This works, BUT, when you add the files to 'Resources', you need to check "Create folder references for any aded folders", and NOT "Create groups for any added folders".

干杯!

这篇关于获取资源文件夹中子目录的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆