码迷,mamicode.com
首页 > 其他好文 > 详细

本地持久化

时间:2016-05-03 23:54:18      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:

将NSString存储在本地
1.需要知道这个对象存在哪里,所以需要一个文件夹的路径

找到Documents文件夹路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

2.创建要存储的内容:字符串

NSString *str = @"stream";

3.需要知道字符串最终存储的地方。所以需要创建一个路径去存储字符串

NSString *strPath = [documentPath stringByAppendingPathComponent:@"MB.txt"];

4.准备将字符串写入文件

[str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

将NSString文件夹存储的内容取出来
将字符串取出:使用stringWithContentsOfFile...encoding...这个方法取出

NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];

将NSData类型的数据存储到本地(以图片为例)

常用的两种初始化image的两种方式

1.使用imageNamed:第一次读取的时候,先把这个图片放到缓存里,下次在使用到这个同名图片的时候直接从缓存里读取;优点:方便快捷,只有第一次使用的时候稍慢,接下来在使用就会稍微快点。缺点:如果在当前工程中只使用一次会浪费内存。

2.使用initWithContentsOfFile初始化图片的时候,每次都会根据路径去读取。不会占用内存。如果图片在当前工程中只使用一次,应该选择这个方法。

UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"/Users/zhaoce/Documents/Senior进阶/Lesson1/Senior_1_简单数据的存储/Senior_1_简单数据的存储/Images/v_red_heart_selected@2x.png"];  //  file填写的参数是图片所在的文件夹路径,将图片show in finder 然后直接拖进来,就是路径。

用UIImageJPEGRepresentation转化为data数据 1代表图片压缩的值是本身

NSData *imageData = UIImageJPEGRepresentation(image, 1);

找到路径进行存储
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
最终路径
NSString *imagePath = [documentPath stringByAppendingString:@"/meng.jpeg"];
[imageData writeToFile:imagePath atomically:YES];

将NSData的数据读取出来,转换成UIIamge类型并显示在imageView上
    
NSData *newImageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *newImage = [UIImage imageWithData:newImageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
imageView.frame = CGRectMake(150, 200, 60, 60);
[self.view addSubview:imageView];

如何把一个person类型的对象存入本地,就是复杂对象的本地化,这个对象必须遵守NSCoding协议,并实现协议中的两个方法。

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;

复杂对象的本地化

1.找到Documents文件夹的目录

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

2.创建Person对象

Person *person = [Person new];
    person.name = @"帅哥";
    person.sex = @"boy";
    person.age = 25;

3.把这个复杂对象归档

3.1步:NSMutableData,用于创建归档工具的
    NSMutableData *data = [NSMutableData data];
3.2步:创建归档工具
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

3.3步:对要归档的person对象进行归档。
    [archiver encodeObject:person forKey:@"key"];

3.4步:结束归档
    [archiver finishEncoding];

4.将归档的内容存储到本地
    NSString *personPath = [documentPath stringByAppendingPathComponent:@"person.plist"];
    [data writeToFile:personPath atomically:YES];

/*******************************************************/

解档

1.将要解档的数据找出
    NSData *resultData = [NSData dataWithContentsOfFile:personPath];

2.创建解档工具
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:resultData];

3.对person对象进行解档
    Person *newPerson = [unarchiver decodeObjectForKey:@"key"];

4.结束解档
    [unarchiver finishDecoding];

 

本地持久化

标签:

原文地址:http://www.cnblogs.com/menglingxu/p/5456892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!