Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:无法从MainBundle复制到NSDocumentDirectory_Iphone_File Io_Nsfilemanager_Nsbundle - Fatal编程技术网

iPhone:无法从MainBundle复制到NSDocumentDirectory

iPhone:无法从MainBundle复制到NSDocumentDirectory,iphone,file-io,nsfilemanager,nsbundle,Iphone,File Io,Nsfilemanager,Nsbundle,我正在尝试将几个图像文件从MainBundle/SampleImages文件夹复制到应用程序NSDocumentDirectory/Library文件夹,但无法执行此操作。我已经检查了图像是否位于.app/Mainbundle/SampleImages目录中,我还检查了NSDocumentsDirectory中是否正在创建库文件夹,但没有复制任何文件 我尝试了以下两种方法,但都没有成功。 这是我用来复制文件的代码 第一种方法 NSArray *paths = NSSearchPathForDir

我正在尝试将几个图像文件从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);
        }
第二种方法

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。。。文件不存在