我不確定爲什麼此代碼無法正常工作。它出現的目錄存在,但後來聲稱它不。目錄未創建
+(NSString *)writeImageToFile:(UIImage *)image {
NSLog(@"image 3: %@", image);
NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path isDirectory:nil]) {
BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
NSLog(@"success 1: %i", success);
}
NSString *name = [NSString stringWithFormat:@"%@.jpg", [JEntry generateUuidString]];
NSString *filePath = [path stringByAppendingPathComponent:name];
NSLog(@"file path: %@", filePath);
NSError *error = nil;
BOOL success = [fullImageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
if (!success) {
NSLog(@"Failed to write to file with error: %@", [error description]);
}
}
我的NSLog:
image 3: <UIImage: 0x6972820>
file path: /var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/3323E0F5-0B13-4CBA-898A-252212C65E2F.jpg
Failed to write to file with error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x6987e20 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/3323E0F5-0B13-4CBA-898A-252212C65E2F.jpg, NSUnderlyingError=0x6977290 "The operation couldn’t be completed. Not a directory"}
您是否嘗試過在目錄路徑中保存沒有圖像?你是否也想創建Images目錄? – Ravin 2012-03-03 15:49:27
你可以發佈你用來創建目錄「圖片」的代碼嗎?另外,你可以發佈我在答案中發佈的代碼的結果嗎? – sch 2012-03-03 16:03:20
嗨,你有解決你的問題嗎?「操作無法完成。不是目錄」請分享您的解決方案,如果可能的話..謝謝! – 2012-09-19 07:49:58