【问题标题】:Accessing all images in a folder iphone Application访问文件夹中的所有图像 iphone 应用程序
【发布时间】:2012-09-03 00:17:00
【问题描述】:

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

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

我想访问文件夹 bagsoldbagsnew 中的所有图像。如果我对 png 使用资源路径和谓词过滤器,它会给我所有 png 文件。有没有办法我可以只访问文件夹中存在的文件。

【问题讨论】:

    标签: iphone ios xcode nsfilemanager


    【解决方案1】:

    只是为了多样化:)

       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;
    

    【讨论】:

      【解决方案2】:

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

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

      See API docs here

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

      See a similar question on Stack Overflow here

      注意上面答案(链接)中的要点,关于您需要在 Xcode 中做什么才能使其工作。

      【讨论】:

      • NSString* bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/Images"]; NSArray* files = [NSBundle pathsForResourcesOfType:@".png" inDirectory:bundlePath]; for ( NSString* fn in files) NSLog(@"%@",fn);这没有返回给我任何图像。就好像我没有将图像附加到资源路径一样,我得到了所有文件夹中的所有图像
      • @ila,我认为您的路径 (@"/Images") 中有一个额外的斜线 (/) ...尽管 可能 并没有什么坏处。另外,您是否注意到我链接到的答案末尾的评论(他说“但是......”)?你这样做了吗?
      【解决方案3】:

      根据您对其他已回答的人的回复,我觉得您将文件夹与 Xcode 组混淆了。 Xcode 组与设备上的文件夹不对应。它们只是为了帮助您为而非设备组织您的 Xcode 项目。任何资源都直接复制到具有平面层次结构的主目录包中。

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

      【讨论】:

        【解决方案4】:

        这样做:

        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
        

        过滤 JPG(如果有)

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

        如果有的话,现在是 PNG

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

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

        将 onlyJPG 和 onlyPNG 合并到一个数组中

        【讨论】:

        • 我在获取 Images/bagsold 文件夹的路径时遇到问题。我怎样才能得到它?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-14
        相关资源
        最近更新 更多