【问题标题】:iPhone/iPad: Unable to copy folder from NSBundle to NSDocumentDirectoryiPhone/iPad:无法将文件夹从 NSBundle 复制到 NSDocumentDirectory
【发布时间】:2011-12-24 23:24:27
【问题描述】:

我正在尝试在我的 NSBundle 中复制一个包含大量图像的文件夹。
我试着用这些代码来做。

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Images"];

    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                          stringByAppendingPathComponent:@"Images"];
        [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];

假设文件夹中有一个名为 Test.png 的图像,我想在我的按钮上显示该图像,但它不起作用!
但是,如果我只将单个图像从 NSBundle 复制到 NSDocumentDirectory,它可以工作!

例子:

变化

stringByAppendingPathComponent:@"Images"  

stringByAppendingPathComponent:@"Test.png"

所以问题在于将文件夹复制到 NSDocumentDirectory!
我上面的代码不正确吗?
还是无法复制文件夹? (这意味着我必须单独复制文件)

【问题讨论】:

    标签: objective-c ios file-io nsfilemanager nsbundle


    【解决方案1】:

    恐怕目前没有办法复制文件夹,如果有第三方功能可以做到这一点,它只会逐个文件复制...因此您必须单独复制每个文件。

    【讨论】:

    • 哦不...这意味着如果我有 50 个文件要复制,我需要编写代码来复制其中的 50 个? :(
    • 恐怕是这样。它看起来像是 geekswordsman 的代码或文件名数组的 foreachloop。
    【解决方案2】:

    Oscar 说得对,目前没有办法用一个命令复制整个文件夹。

    这样的方法可能会奏效(警告 - 我的办公室连接已断开,我无法访问我的 Mac 来验证此代码是否正常工作。一旦我能够验证这一点)。只需调用 [self copyDirectory:@"Images"],它就会处理其余部分。

    -(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];
        } else {
            NSLog(@"Directory exists! %@", documentDBFolderPath);
        }
    
        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 {
                NSLog(@"File exists: %@", newFilePath);
            }
        }
    }
    

    -- 编辑 2011 年 11 月 9 日-- 对此感到抱歉,因为我预先警告过我无法访问我的 mac 来验证代码。 更新了代码,我已经验证它现在可以正常工作。添加了几个快速的 NSLogs 来告诉你新目录或新文件是否已经存在。

    【讨论】:

    • 它说 directoryContentsAtPath 已弃用。
    • 而且它不起作用。我尝试从 NSDocumentDirectory 检索图像并将其显示到图像视图,但它不起作用。
    • @Lloydworth - 为您更新了代码,它在 iOS 5.0 上正常工作。如果您有任何问题或需要帮助,请告诉我!
    【解决方案3】:

    下面的代码将在文档目录下创建一个名为“images”的文件夹,并将捆绑中名为“folderinbundle”的文件夹中的所有文件复制到“images”

    - (void) copyImages
    {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"folderinbundle"];  //folder contain images in your bundle
        NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"images"];  //images is your folder under document directory
    
        NSError *error;
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&error];  //copy every files from sourcePath to destPath
    }
    

    编辑澄清:如果“folderinbundle”是物理文件夹而不是组,则上述内容将起作用。

    【讨论】:

    • 我已经删除了我之前关于这不起作用的评论。它完美地工作。谢谢
    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2017-04-13
    • 2011-04-14
    • 2011-05-02
    • 1970-01-01
    • 2012-01-03
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多