"The operation couldn’t be completed. (Cocoa error 512.)"
Solution 1
I was able to reproduce your error when writing a file first in the path @"Documents/Images/"
, then trying to write the image using your code.
I think there are two possible scenarios for this:
1) You created that file by mistake at a previous execution of your app. This will be solved if you reset the simulator using the menu: iOS Simulator > Reset Contents and Settings, and uninstalling the app from your device: Long press > click on the x symbol.
2) There is some code somewhere else in your app that creates this file. If this is the case, you should find this code and remove it.
Solution 2
From FoundationErrors.h:
NSFileWriteUnknownError = 512
Try using withIntermediateDirectories:YES
.
Solution 3
In my case a period '.' in the directory name (e.g. ~/Documents/someDir.dir/somefile) was the cause of the problem. I removed the offending character and the error disappeared.
Andrew
Updated on June 21, 2022Comments
-
Andrew almost 2 years
I have this code, which should be working perfectly, but I can't udnerstand why it isn't:
+(NSString *)writeImageToFile:(UIImage *)image { NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f); NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDirectory = NO; BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory]; if (directoryExists) { NSLog(@"isDirectory: %d", isDirectory); } else { NSError *error = nil; BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error]; if (!success) { NSLog(@"Failed to create directory with error: %@", [error description]); } } NSString *name = [NSString stringWithFormat:@"%@.jpg", [JEntry generateUuidString]]; NSString *filePath = [path stringByAppendingPathComponent:name]; NSError *error = nil; BOOL success = [fullImageData writeToFile:filePath options:NSDataWritingAtomic error:&error]; if (!success) { NSLog(@"Failed to write to file with error: %@", [error description]); } return filePath; }
It passed the directoryExists without an error, but when it gets to writeToFile, it gives me this error:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x5634ee0 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/18DAE0BD-6CB4-4244-8ED1-9031393F6DAC.jpg, NSUnderlyingError=0x5625010 "The operation couldn’t be completed. Not a directory"}
Any ideas why this might be?
-
Robert over 11 years@sch: Late in the day, I know, but thank you for this. My issue was my code was running fine on the iPad but not on the simulator. Reset Contents and Settings solved the problem.
-
coolcool1994 almost 11 yearsI had problem 2). But in the first place, compiler says there is no persistent store. Now I tries to add a persistent store, it fails with cocoa error 512 because I already have! What a !
-
trainoasis almost 10 years@sch do you perhaps have any clues about my similar issue? stackoverflow.com/questions/24610313/app-cache-ios-phonegap