Reputation: 4441
I tried saving a file into NSLibraryDirectory with the below code:
if (!userDir) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [[[documentsDirectory stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:@"account"] stringByAppendingPathComponent:@"test"];
NSString *imageDir = [[documentsDirectory stringByAppendingPathComponent:@"Root"] stringByAppendingPathComponent:@"imageType"];
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:&error];
NSString *fileName = @"avatar"
NSString filePath = [imageDir stringByAppendingPathComponent:fileName];
}
NSData *imagedata = **imagedata**
NSError *saveError = nil;
[imageData writeToFile:filePath options:NSDataWritingWithoutOverwriting error:&saveError];
Image is saved and I can load it using the following method:
NSError *error = nil;
NSData *imageData = [[NSData alloc] initWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];
When I kill the app and launch it again, the file is missing. Am I doing anything wrong here?
Upvotes: 0
Views: 107
Reputation: 10788
You're saving it in the Caches directory, which the system may clear whenever your app is not running. If you want the file to stick around more permanently you should store it somewhere else, like the Documents directory.
Upvotes: 1