文件夹从NSBundle复制到NSDocumentDirecto

文件夹从NSBundle复制到NSDocumentDirecto

本文介绍了iPhone / iPad:无法将文件夹从NSBundle复制到NSDocumentDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复制一个文件夹在我的NSBundle包含相当多的图像。

我试着用这些代码。

  NSFileManager * fileManager = [NSFileManager defaultManager]; 
NSError *错误;
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的单个图像,它的工作!


$ b 示例: / b>

更改

  stringByAppendingPathComponent:@Images



  stringByAppendingPathComponent :@Test.png

所以问题在于将文件夹复制到NSDocumentDirectory!

上面的代码是不正确的吗?

或者复制文件夹是不可能的? (这意味着我必须单独复制文件)

解决方案

奥斯卡是正确的,目前没有办法复制整个文件夹一个单一的命令。

像这样的方法可能会诀窍(预警 - 我的办公室连接已关闭,我无法访问我的Mac来验证此代码正在工作一旦我能够验证这一点)。只需要调用[self copyDirectory:@Images],它就可以处理剩下的事情。

   - (void)copyDirectory :( NSString *)目录{
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError *错误;
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
NSString * resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory]; $!
$ b if(![fileManager fileExistsAtPath:documentDBFolderPath]){
//创建目录!
[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]){
//文件不存在,复制
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:& error];
} else {
NSLog(@File exists:%@,newFilePath);





- 编辑11/9 / 2011 -
对不起,因为我预先警告我无法访问我的Mac来验证代码。
更新了代码,我已经验证它现在正常工作。添加了几个快速的NSLogs来告诉你新的目录或新的文件是否已经存在。


I am trying to copy a folder in my NSBundle which contains quite a number of images.
I tried doing it with these codes.

    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];

Lets say there's an image in the folder named Test.png and i want to display the image on my button, it does not work!
However, if i only copied a single image from the NSBundle to NSDocumentDirectory, it works!

Example:

Changing

stringByAppendingPathComponent:@"Images"

To

stringByAppendingPathComponent:@"Test.png"

So the problem lies with copying the folder to NSDocumentDirectory!
Are my codes above incorrect?
Or it is impossible to copy folders? (Which means i have to copy files individually)

解决方案

Oscar is correct, there is currently no way to copy an entire folder with a single command.

A method such as this might do the trick (forewarning - my office connection is down and I can't access my Mac to verify this code is working. Will verify this once I am able). Simply call with [self copyDirectory:@"Images"] and it will handle the rest.

-(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);
        }
    }
}

-- EDIT 11/9/2011 --Sorry about that, as I forewarned I was not able to access my mac to verify the code.Updated the code and I have verified it is working properly now. Added a couple quick NSLogs to tell you if the new directory or the new file already exists.

这篇关于iPhone / iPad:无法将文件夹从NSBundle复制到NSDocumentDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:34