获取目录BundlePath内的内容列表

时间:2014-02-26 10:52:38

标签: ios iphone objective-c ios7 nsbundle

  • 我正在开发项目,其中我需要自定义图像库 图像仅来自特定目录。
  • 所以,我在文档目录和副本列表中创建了一个目录 来自Bundle path的文件名为“ImageFolder”的目录,如图所示 屏幕截图。

  • 我尝试了不同的代码,但我总是得到数组nil.I知道路径上没有文件夹,但我该怎么办请告诉我。

- 我想要的>

  1. 在文档目录中创建目录“xyz”。

  2. 将所有文件从“ImageFolder”复制到“xyz”。

  3. enter image description here

    // ============================================= ===================

    enter image description here

    // ============================================= ===================

    enter image description here

3 个答案:

答案 0 :(得分:1)

您显示的ImageFolder屏幕截图是Xcode项目中的一个组,但这并不意味着与该组相关的任何内容都会被复制到应用中。如果该应用程序中的图像位于您的副本包资源构建阶段,那么它们将被直接复制到应用程序包(而不是子文件夹)。

您需要添加新的复制文件构建阶段,设置文件夹名称,并将图像移动到该文件夹​​中。

完成后,请考虑复制图像的原因。用户可以进行一些编辑吗?如果没有那么复制图像是浪费空间。如果用户可以“删除”某些图像,则考虑存储“已删除项目”的plist或用户默认值,以隐藏/过滤图像列表。

答案 1 :(得分:1)

此代码有效

-(void) copyDirectory:(NSString *)directory {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory];

    if (![fileManager fileExistsAtPath:documentDBFolderPath]) {

        //Create Directory!
        [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error];

        NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error];
        for (NSString *s in fileList) {
            NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s];
            NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s];
            if (![fileManager fileExistsAtPath:newFilePath]) {
                //File does not exist, copy it
                [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
            } else {
                [fileManager removeItemAtPath:newFilePath error:&error];
                [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
            }
        }

        [self info:@"Copy finish!"];
    }
}

在目录中添加要复制的文件夹的名称

编辑:

您的文件夹必须通过引用添加。将文件夹拖到xCode中的项目中,然后选择“为任何添加的文件夹创建文件夹引用”

enter image description here

答案 2 :(得分:0)

您目前拥有的是一个组(黄色文件夹图标)(实际上它们不在子文件夹中) - 您需要做的是在Xcode外部创建一个文件夹,输入您需要的图像然后将其拖入您的project - 你应该有一个蓝色文件夹图标,你的命令应该可以工作。

另外一件事,只是引用它,这样你就不必每次将它们提取到Xcode时继续将它们引入Xcode,然后将它编译并放入包中。