我有這樣的代碼,它應該工作完美,但我不能udnerstand爲什麼它不是:「操作無法完成(可可錯誤512)。」
+(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;
}
它通過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"}
任何想法,這可能是爲什麼?
看來第一個解決方案是正確的。感謝那。 :) – Andrew 2012-03-03 17:43:35
@sch:我知道那天晚些時候,但是非常感謝你。我的問題是我的代碼在iPad上運行良好,但不在模擬器上運行。重置內容和設置解決了這個問題。 – Robert 2012-11-27 16:22:54
我有問題2)。但首先,編譯器說沒有持久存儲。現在我嘗試添加一個持久性存儲,因爲我已經擁有可可錯誤512而失敗!好! – coolcool1994 2013-07-02 14:27:47