17
我嘗試使用下面的代碼保存圖像:錯誤數據保存到緩存目錄(iPhone)
- (void)writeData{
if(cacheFileName==nil)
return;
if(cacheDirectoryPath==nil)
return;
if (![[NSFileManager defaultManager] fileExistsAtPath:[self filePath]]) {
NSData *imageData = UIImageJPEGRepresentation(self.image, 0.9);
NSError *writeError= nil;
BOOL didWrite = [imageData writeToFile:[self filePath] options:NSAtomicWrite error:&writeError];
if(writeError)
NSLog([writeError localizedDescription]);
if(didWrite)
NSLog(@"image saved");
else
NSLog(@"image not saved");
}
}
- (NSString *)filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *filename = [cacheDirectory stringByAppendingPathComponent:cacheDirectoryPath];
filename = [filename stringByAppendingPathComponent:cacheFileName];
NSLog(filename);
return filename;
}
我得到一個錯誤:
2009-06-23 16:39:19.740 XXX[33454:20b] Operation could not be completed. (Cocoa error 4.)
我看這個,看看錯誤具有以下含義:
NSFileNoSuchFileError = 4, // Attempt to do a file system operation on a non-existent file
這是沒有意義的,因爲該文件不應該存在。
我試圖寫PNG表示和使用的便利方法:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
我使用類似的代碼保存的Plist,他們很好地工作。我不確定問題是什麼。有任何想法嗎?
它看起來像你保存到緩存目錄裏面的一些目錄中創建它們。 (cacheDirectory/** cacheDirectoryPath ** /文件名)是否創建了intermediare dirs? – epatel 2009-06-23 20:55:07