xcode 访问文件夹中的所有图像 iphone 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12346473/
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 01:26:38  来源:igfitidea点击:

Accessing all images in a folder iphone Application

iphoneiosxcodensfilemanager

提问by ila

I am making an iPhone app using Xcode and following is the directory structure

我正在使用 Xcode 制作 iPhone 应用程序,以下是目录结构

GFGGAME
  -gfggame.xcodeproj
  -Images
    --bagold
     ---bags_1.png
     ---bags_2.png
    --bagsnew
     ---bags_5.png
     ---bags_6.png

I want to access all images from folder bagsoldand bagsnew. If I use resource path and a predicate filter for png it gives me all the png files . Is there a way i can access just the files present in the folder.

我想访问文件夹bagsoldbagsnew. 如果我对 png 使用资源路径和谓词过滤器,它会为我提供所有 png 文件。有没有办法只访问文件夹中的文件。

采纳答案by Nate

I think you probably want to use this NSBundlemethod:

我想你可能想使用这种NSBundle方法:

+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath

See API docs here

在此处查看 API 文档

You would pass @".png"as your extension, and then specify the directory path just for the subdirectories you want (you might need to call this twice, one for each subdirectory, and then just append the second array of paths to the first).

您将@".png"作为扩展传递,然后仅为所需的子目录指定目录路径(您可能需要调用两次,每个子目录调用一次,然后将第二个路径数组附加到第一个)。

See a similar question on Stack Overflow here

在此处查看 Stack Overflow 上的类似问题

Notethe point in the answer (link) above, about what you need to do in Xcode to make this work.

请注意上面答案(链接)中的要点,即您需要在 Xcode 中执行哪些操作才能完成这项工作。

回答by Jim

From your responses to the other people who have answered, it sounds to me like you've confused folders with Xcode groups. Xcode groups do not correspond to folders on the device. They are solely there to help you keep your Xcode project organised for you, not the device. Any resources just get copied straight into the main directory bundle with a flat hierarchy.

从您对其他回答者的回复来看,在我看来,您似乎将文件夹与 Xcode 组混淆了。Xcode 组与设备上的文件夹不对应。他们只是在那里帮助你保持你的Xcode项目组织了,而不是设备。任何资源都会直接复制到具有平面层次结构的主目录包中。

When you drag a folder into Xcode to add it to a project, you need to select "Create folder references for any added folders"and not"Create groups for any added folders". This will preserve the directory layout when the application is built.

当您将文件夹拖入 Xcode 以将其添加到项目时,您需要选择“为任何添加的文件夹创建文件夹引用”不是“为任何添加的文件夹创建组”。这将在构建应用程序时保留目录布局。

回答by Paresh Navadiya

Do this:

做这个:

NSString *bundleRootpath = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [bundleRootpath pathForResource:@"bags_1" ofType:@"png" inDirectory:@"Images/bagold"]
NSFileManager *fileMangr = [NSFileManager defaultManager];
NSArray *dirContents = [fileMangr contentsOfDirectoryAtPath:bundleRootpath error:nil]; //your path here it may be document directory also

Filter JPG if any

过滤JPG(如果有)

NSArray *onlyJPGs;
if([dirContents count] > 0){
NSPredicate *fltrJPG = [NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"];
onlyJPGs = [dirContents filteredArrayUsingPredicate:fltrJPG];
}

Now PNG if any

现在PNG(如果有的话)

NSArray *onlyPNGs;
if([dirContents count] > 0){
NSPredicate *fltrPNG = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
onlyPNGs = [dirContents filteredArrayUsingPredicate:fltrPNG];

Search for any other format if any

搜索任何其他格式(如果有)

Merge onlyJPGs and onlyPNGs into one array

将 onlyJPGs 和 onlyPNGs 合并为一个数组

回答by DarkoM

Just for variety :)

只是为了多样性:)

   NSMutableArray *filePaths = [NSMutableArray arrayWithArray:[NSBundle pathsForResourcesOfType:nil inDirectory:fullPathToFolder]];
    NSMutableArray *toDelete = [NSMutableArray array];
    if(filePaths.count>0){
        [filePaths enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
            if(!([obj hasSuffix:@"jpg"] || [obj hasSuffix:@"png"])){
                    [toDelete addObject:obj];
            }
        }];
    }
    [filePaths removeObjectsInArray:toDelete];
    return filePaths;