[英]“The operation couldn’t be completed. (Cocoa error 512.)”
I have this code, which should be working perfectly, but I can't udnerstand why it isn't: 我有这个代码,应该是完美的工作,但我不能udnerstand为什么它不是:
+(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: 它传递了directoryExists而没有错误,但是当它到达writeToFile时,它给了我这个错误:
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? 任何想法为什么会这样?
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. 在路径
@"Documents/Images/"
首先写入文件时,我能够重现您的错误,然后尝试使用您的代码编写图像。
I think there are two possible scenarios for this: 我认为有两种可能的情况:
1) You created that file by mistake at a previous execution of your app. 1)您之前执行的应用程序错误地创建了该文件。 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 .
如果您使用以下菜单重置模拟器,将解决此问题: iOS模拟器>重置内容和设置 ,以及从您的设备卸载应用程序: 长按>单击x符号 。
2) There is some code somewhere else in your app that creates this file. 2)您的应用程序中的某个地方有一些代码可以创建此文件。 If this is the case, you should find this code and remove it.
如果是这种情况,您应该找到此代码并将其删除。
From FoundationErrors.h : 来自FoundationErrors.h :
NSFileWriteUnknownError = 512
Try using withIntermediateDirectories:YES
. 尝试使用
withIntermediateDirectories:YES
。
In my case a period '.' 在我的情况下,一段时间'。' in the directory name (eg ~/Documents/someDir.dir/somefile) was the cause of the problem.
在目录名称(例如〜/ Documents / someDir.dir / somefile)是问题的原因。 I removed the offending character and the error disappeared.
我删除了违规字符,错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.