我如何保存在NSFileManager中?

时间:2015-07-04 17:00:28

标签: ios nsfilemanager

我有一个使用json服务的应用程序,我想在NSFileManager中保存这个json,该测量最多可以有多少?尺寸有限吗?

1 个答案:

答案 0 :(得分: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.