iPhone:无法从MainBundle复制到NSDocumentDirectory
我正在尝试将几个图像文件从MainBundle/SampleImages文件夹复制到应用程序NSDocumentDirectory/Library文件夹,但无法执行此操作。我已经检查了图像是否位于.app/Mainbundle/SampleImages目录中,我还检查了NSDocumentsDirectory中是否正在创建库文件夹,但没有复制任何文件 我尝试了以下两种方法,但都没有成功。 这是我用来复制文件的代码 第一种方法iPhone:无法从MainBundle复制到NSDocumentDirectory,iphone,file-io,nsfilemanager,nsbundle,Iphone,File Io,Nsfilemanager,Nsbundle,我正在尝试将几个图像文件从MainBundle/SampleImages文件夹复制到应用程序NSDocumentDirectory/Library文件夹,但无法执行此操作。我已经检查了图像是否位于.app/Mainbundle/SampleImages目录中,我还检查了NSDocumentsDirectory中是否正在创建库文件夹,但没有复制任何文件 我尝试了以下两种方法,但都没有成功。 这是我用来复制文件的代码 第一种方法 NSArray *paths = NSSearchPathForDir
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);
}
第二种方法
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我通过改变第二种方法来完成它。 不需要创建目录,因为SDK文档说明目标目录必须不存在
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"SampleImages"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
无用:错误域=NSCOCAERRORDOMAIN代码=260。。。文件不存在