简体   繁体   English

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

[英]Accessing all images in a folder iphone Application

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 bagsold and bagsnew . 我想访问文件夹bagsoldbagsnew所有图像。 If I use resource path and a predicate filter for png it gives me all the png files . 如果我使用资源路径和png的谓词过滤器,它会给我所有的png文件。 Is there a way i can access just the files present in the folder. 有没有办法可以访问文件夹中的文件。

I think you probably want to use this NSBundle method: 我想你可能想要使用这个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的类似问题

Note the point in the answer (link) above, about what you need to do in Xcode to make this work. 请注意上面答案(链接)中的要点,关于在Xcode中需要做什么才能使其工作。

From your responses to the other people who have answered, it sounds to me like you've confused folders with Xcode groups. 从您对其他已回答的人的回复中,听起来像是您将文件夹与Xcode组混淆了。 Xcode groups do not correspond to folders on the device. Xcode组与设备上的文件夹不对应。 They are solely there to help you keep your Xcode project organised for you , not the device. 他们只是在那里帮助你保持组织你的Xcode项目,而不是设备。 Any resources just get copied straight into the main directory bundle with a flat hierarchy. 任何资源都会直接复制到具有平面层次结构的主目录包中。

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" . 将文件夹拖入Xcode以将其添加到项目时,需要选择“为任何添加的文件夹创建文件夹引用”不是 “为任何添加的文件夹创建组” This will preserve the directory layout when the application is built. 这将在构建应用程序时保留目录布局。

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 仅将JPG和仅PNG合并为一个数组

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM