2015-07-04 25 views
0

我有一個應用程序使用json服務,我想保存在NSFileManager這個度量,可以有至多?大小有限制?我如何保存在NSFileManager中?

+0

你不要使用NSFileManager來保存文件。請澄清你正在嘗試做什麼,你嘗試過什麼,以及你有什麼問題。 – rmaddy

+0

你總是可以存檔整個結構(假設每個葉符合'NSCoding'協議),並且任何時候你都可以從文件中恢復它。 – holex

回答

0

爲了節省:

UIImage *pickedImage = [UIImage imageNamed:@"anyImageName"]; 
NSData *pngData = UIImagePNGRepresentation(pickedImage); 
NSString *withFullPathName = @"yourFullPathFileName"; 
[pngData writeToFile:withFullPathName atomically:YES]; 

一旦你保存你的數據(想象中的照片),所以你可以使用的NSFileManager閱讀:

NSData* data; 
NSString *fullPathName = @"yourFullPathFileName"; 
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPathName]) { 

    data = [[NSData alloc] initWithContentsOfFile:fullPathName]; 
    UIImage *image = [UIImage imageWithData:data]; 
} else { 
    NSLog(@"file does not exist."); 
} 

// When you set your fullPathName try make sure it is unique for each file. 

編輯

// Considering your JSON is a NSString: 

// 1. Convert the nsstring to nsdata 
NSString* str = @"yourJsonAsString:{...}"; 
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; 

// You don't use nsfilemanager to save you use to read, you use "writeToFile:" to store your info. 
[data writeToFile:@"yourFullPathName" atomically:YES]; 

// To read your file, use the example of the code above about NSFileManager. 
// About the size, the only size limit is the amount of available space on the device. However is not a good pratice save a huge file. You should use core-data. 
+0

寫入和讀取文件時,您確實需要指定完整路徑。 – rmaddy

+0

嗨@rmaddy感謝您的評論。我改進了我的答案。 –

+0

我想在NSFilemanager中保存一個JSON,有可能嗎?如果答案是YES,那麼最大尺寸是多少? –