Llo*_*rth 3 file-io objective-c nsfilemanager nsbundle ios
我想在我的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];
Run Code Online (Sandbox Code Playgroud)
让我们说在名为Test.png的文件夹中有一个图像,我想在我的按钮上显示图像,它不起作用!
但是,如果我只将NSBundle中的单个图像复制到NSDocumentDirectory,它就可以了!
例:
更改
stringByAppendingPathComponent:@"Images"
Run Code Online (Sandbox Code Playgroud)
至
stringByAppendingPathComponent:@"Test.png"
Run Code Online (Sandbox Code Playgroud)
所以问题在于将文件夹复制到NSDocumentDirectory!
我的代码是否错误?
或者是不可能复制文件夹?(这意味着我必须单独复制文件)
下面的代码将在文档目录下创建一个名为"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
}
Run Code Online (Sandbox Code Playgroud)
编辑以澄清:如果"folderinbundle"是物理文件夹而不是组,则上述操作将起作用.
奥斯卡是正确的,目前没有办法用一个命令复制整个文件夹.
像这样的方法可能会起作用(预警 - 我的办公室连接已关闭,我无法访问我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
- 编辑11/9/2011 - 对此我很抱歉,因为我预先警告我无法访问我的mac来验证代码.更新了代码,我已经验证它现在正常工作.添加了几个快速NSLog,告诉您新目录或新文件是否已存在.
| 归档时间: |
|
| 查看次数: |
2214 次 |
| 最近记录: |