1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 #pragma mark - 简单对象的本地持久化 13 #pragma mark - 将NSString类型的数据存储到本地 14 15 // 1.需要知道这个对象存在哪里,所以需要一个文件夹的路径 16 // 找到Documents文件夹路径 17 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 18 19 // 2.我们知道要存储什么?所以创建要存储的内容 20 // 创建要存储的内容:字符串 21 NSString *str = @"AJAR"; 22 23 // 3.需要知道字符串最终存储的地方,所以需要创建一个路径去存储字符串 24 NSString *strPath = [documentPath stringByAppendingPathComponent:@"yinhao.txt"]; 25 NSLog(@"%@", strPath); 26 27 // 4.准备工作完成,将字符串写入文件 28 // 第一个参数:写入的文件的路径 29 // 第二个参数:在断电的时候,会不会自动保存 30 [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 31 32 33 // 将字符串取出:stringWithContentsOfFile这个函数将其取出 34 // 第一个参数:字符串存储的路径 35 // 第二个参数:编码格式 36 // 第三个参数:错误信息 37 NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil]; 38 NSLog(@"%@", newStr); 39 40 41 #pragma mark - 将NSArray类型的数据存储到本地 42 43 // 1.找到Documents文件夹路径 44 NSString *documentPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 45 46 // 2.创建一个数组 47 NSArray *array = @[@"Black", @"MBBoy", @"BPY", @"SPY", @"SeaRoot", @"BOOM"]; 48 49 // 3.创建数组存储的最终路径 50 NSString *arrayPath = [documentPath1 stringByAppendingPathComponent:@"yinhao.plist"]; 51 52 // 4.写入本地 53 [array writeToFile:arrayPath atomically:YES]; 54 NSLog(@"%@", arrayPath); 55 56 // 将存储在本地的数据取出 57 NSArray *newArray = [NSArray arrayWithContentsOfFile:arrayPath]; 58 NSLog(@"%@", newArray); 59 60 61 #pragma mark - 将NSDictionary类型的数据存储到本地 62 63 // 1.找到Documents文件夹路径 64 NSString *documentPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 65 66 // 2.创建一个字典 67 NSDictionary *dictionary = @{@"name" : @"yinhao", @"gender" : @"man", @"age" : @"18", @"hobby" : @"NBA"}; 68 69 // 3.创建字典存储的最终路径 70 NSString *dictionaryPath = [documentPath2 stringByAppendingPathComponent:@"zhizunbao.plist"]; 71 72 // 4.写入本地 73 [dictionary writeToFile:dictionaryPath atomically:YES]; 74 NSLog(@"%@", dictionaryPath); 75 76 // 将存储在本地的数据取出 77 NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath]; 78 NSLog(@"%@", newDict); 79 80 81 #pragma mark - 将NSData类型的数据存储到本地(以UIImage为例) 82 83 // 常用的两种初始化image的方式 84 // imageNamed:第一次使用的时候,先把这个照片放到缓存里,下次使用到这个同名图片的时候直接从缓存中读取;优点:方便快捷,只有第一次使用的时候稍慢,接下来再使用就会稍微快点;缺点:如果在当前的工程中只使用一次会浪费内存 85 // initWithContentsOfFile:每次都会根据路径去读取,不会占用内存,如果图片在当前工程中只使用;一次,应该选择这个方法 86 87 UIImage *image = [UIImage imageNamed:@"000.jpg"]; 88 // UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"路径"]; 89 90 /* 91 123.png 92 123@2x.png 93 123@3x.png 94 图片适配的相关内容 95 */ 96 97 // 将image类型的对象转换成NSData类型的数据进行存储 98 // 使用UIImageJPEGRepresentation将图片转换为NSData类型的数据 99 // 第一个参数:要转换的image对象 100 // 第二个参数:表示图片压缩的值 101 // iPhone中将大于2Md图片,会自动旋转90度,所以最终会将图片保存成jpeg格式的图片 102 NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 103 104 // 找到路径进行存储 105 NSString *documentPath3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 106 107 // 最终路径 108 NSString *imagePath = [documentPath3 stringByAppendingPathComponent:@"123.jpeg"]; 109 110 // 将imageData写入 111 [imageData writeToFile:imagePath atomically:YES]; 112 NSLog(@"%@", imagePath); 113 114 115 // 读取NSData类型的数据 116 // 需求:将NSData类型的数据读取出来,转换成UIImage类型的数据显示在imageView上; 117 NSData *newData = [NSData dataWithContentsOfFile:imagePath]; 118 UIImage *showImage = [UIImage imageWithData:newData]; 119 120 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, self.view.frame.size.height - 200)]; 121 [self.view addSubview:imageView]; 122 imageView.image = showImage; 123 124 } 125 @end