3

MainBundle/SampleImages フォルダからアプリケーションの NSDocumentDirectory/Library フォルダにいくつかの画像ファイルをコピーしようとしていますが、コピーできません。画像が .app/Mainbundle/SampleImages ディレクトリにあることを確認し、Library フォルダーが NSDocumentsDirectory に作成されていることも確認しましたが、ファイルはコピーされません。

以下に示す2つのアプローチを試しましたが、成功しませんでした。これは、ファイルをコピーするために使用しているコードです。

最初の方法

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];

2番目の方法

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);
        }

解決策を探すのに数時間を費やしました。ここで何か助けていただければ幸いです。

ありがとうございました

シュマイス・ウル・ハク

4

1 に答える 1

2

少し変更することで、2番目の方法でそれを完了しました。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];
于 2010-07-26T02:40:25.840 に答える