【问题标题】:iPhone : Unable to copy from MainBundle to NSDocumentDirectoryiPhone:无法从 MainBundle 复制到 NSDocumentDirectory
【发布时间】: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


    【解决方案1】:

    我通过改变它用第二种方法完成了它。 不需要创建目录,因为 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];
    

    【讨论】:

    • 无用:错误域=NSCocoaErrorDomain Code=260 ...文件不存在
    猜你喜欢
    • 2011-12-24
    • 2012-03-16
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2012-11-04
    • 2019-12-24
    相关资源
    最近更新 更多