【发布时间】:2011-03-20 21:30:49
【问题描述】:
我正在尝试将几个图像文件从我的 MainBundle/SampleImages 文件夹复制到应用程序 NSDocumentDirectory/Library 文件夹,但我无法这样做。我检查了图像是否在 .app/Mainbundle/SampleImages 目录中,并且我还检查了库文件夹是否正在 NSDocumentsDirectory 中创建,但没有文件被复制。
我尝试了以下两种方法,但均未成功。 这是我用来复制文件的代码。
第一种方法
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];
NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImages"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
[[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
contents:mainBundleFile
attributes:nil];
第二种方法
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentSampleImagesFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];
NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImages/"];
[fileManager createDirectoryAtPath: documentSampleImagesFolderPath attributes:nil];
if([fileManager copyItemAtPath:resourceSampleImagesFolderPath toPath:documentSampleImagesFolderPath error:&error]) {
NSLog(@"success");
}
else {
NSLog(@"Failure");
NSLog(@"%d",error);
}
我已经花了几个小时寻找解决方案,我们将不胜感激。
谢谢
Shumais Ul Haq
【问题讨论】:
标签: iphone file-io nsfilemanager nsbundle