![](/img/trans.png)
[英]The operation couldn’t be completed. (Cocoa error 512.) Coredata Iphone. No files can be copied in documents folder
[英]“The operation couldn’t be completed. (Cocoa error 512.)”
我有這個代碼,應該是完美的工作,但我不能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;
}
它傳遞了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"}
任何想法為什么會這樣?
在路徑@"Documents/Images/"
首先寫入文件時,我能夠重現您的錯誤,然后嘗試使用您的代碼編寫圖像。
我認為有兩種可能的情況:
1)您之前執行的應用程序錯誤地創建了該文件。 如果您使用以下菜單重置模擬器,將解決此問題: iOS模擬器>重置內容和設置 ,以及從您的設備卸載應用程序: 長按>單擊x符號 。
2)您的應用程序中的某個地方有一些代碼可以創建此文件。 如果是這種情況,您應該找到此代碼並將其刪除。
來自FoundationErrors.h :
NSFileWriteUnknownError = 512
嘗試使用withIntermediateDirectories:YES
。
在我的情況下,一段時間'。' 在目錄名稱(例如〜/ Documents / someDir.dir / somefile)是問題的原因。 我刪除了違規字符,錯誤消失了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.