The call to NSFileManager fileExistsAtPath:isDirectory:
gives you an indeterminate value for isDir
if the directory doesn't exist. You should change your code to:
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:pieceImagesDirectory isDirectory:&isDir]) {
[[NSFileManager defaultManager]createDirectoryAtPath:pieceImagesDirectory withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"Error after creating directory:\n%@",error);
} else {
// file exists - I don't expect to use the else block. This is for figuring out what's going on.
NSLog(@"File %@ exists -- is it a directory? %@",pieceImagesDirectory, isDir?@"YES":@"NO");
}
It also appears that you add a second folder User_Image
to the path. You never create this directory.
I would also suggest you change how you write the image data. Instead of using NSFileManager createFileAtPath:contents:attributes:
, use NSData writeToFile:options:error:
. Then you can get an error object providing more details of any problem.
In the end it may be best to build the full path to the file. Strip off the last path component (the actual filename) and then check for the existence of the remaining path. Then make one call to createDirectoryAtPath...
and let it create any all all needed folders.