iPhone / iPad:无法将文件夹从NSBundle复制到NSDocumentDirectory

我试图复制一个文件夹在我的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的文件夹中有一个图像,我想在我的button上显示图像,这是行不通的!
但是,如果我只从NSBundle复制到NSDocumentDirectory的单个图像,它的工作原理!

例:

更改

 stringByAppendingPathComponent:@"Images" 

 stringByAppendingPathComponent:@"Test.png" 

所以问题在于复制文件夹到NSDocumentDirectory!
我的代码是否错误?
或者不可能复制文件夹? (这意味着我必须单独复制文件)

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

像这样的方法可能会诀窍(预警 – 我的办公室连接closures,我无法访问我的Mac来validation此代码是否正常工作,一旦我能够validation这一点)。 只需使用[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); } } } 

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

下面的代码将在文档目录下创build一个名为“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”是一个物理文件夹而不是一个组,上述将工作。

恐怕目前还没有办法复制文件夹,如果有第三方function的话,它所做的只是复制文件…因此,您将不得不单独复制每个文件。