I need to copy some png to documents folder from the main bundle at the first run of my app. I need it because I have to save the file path of these images in a core data entity.
I tried in that way, but it don't works, anybody knows why?
// to copy image from main bundle to documents folder
NSString* mainBundleFilePath = [[NSBundle mainBundle] pathForResource:@"SbriciolataDiRicotta" ofType:@"png"];
NSArray *destPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [destPath objectAtIndex:0];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath];
[[NSFileManager defaultManager] createFileAtPath:documentsDirectory
contents:mainBundleFile
attributes:nil];
// to save the file path
NSString*nomeImmagine = [[NSString alloc] initWithFormat:@"SbriciolataDiRicotta"];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@.png",documentsDirectory, nomeImmagine];
Thanks
createFileAtPath
expects as argument the path of file to be created, not the path
of the enclosing directory.
Remarks:
copyItemAtPath:toPath:error:
to copy the file without loading
the entire contents into memory.initWithFormat
to assign a constant string is unnecessary.stringByAppendingPathComponent:
is the correct method to append a path component.