数据处理之文件读写

沙盒机制

  • 每一个应用程序都会有一个应用程序沙盒
  • 应用程序沙盒就是一个文件系统目录
  • 查找某个应用程序的沙盒
  • 获取Documents目录

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

     NSString *documentsDirectory = [paths objectAtIndex:0];

  • 获取程序包中一个图片资源(app.png)路径方法:

     NSString *imagePath = [NSBundle mainBundle]pathForResource:@"app"ofType@"png";

简单对象的读写

  • #pragma mark - 将NSString类型的数据存储到本地
        
        //1:需要知道这个对象存在哪里,所以需要一个文件夹的路径
        //找到Documents文件夹路径
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        //2:我们知道要存储什么?所以需要创建什么
        //创建要存储的内容:字符串
        NSString *str = @"AJAR";
        
        //3.需要知道字符串最终存储的地方,所以需要创建一个路径去存储字符串
        NSString *strPath = [documentPath stringByAppendingPathComponent:@"wangyuanyuan.txt"];
        
        
        //4.准备工作完成,将字符串写入文件
        //第一个参数:写入的文件的一个路径
        //第二个参数:在断电的情况下,会不会自动保存
        [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        NSLog(@"strPath = %@",strPath);
        
        
    #pragma mark - 将NSString文件夹存储的内容取出来
        //将字符串取出:使用stringWithContentsOfFile这个方法将其取出
        //第一个参数:字符串存储的路径
        //第二个参数:编码格式
        //第三个参数:错误信息
        NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
        
        NSLog(@"newStr = %@",newStr);
    #pragma mark - 将NSData类型的数据存储到本地(以图片为例)
        
        //常用的两种初始化image的两种方式
        //1.使用imageNamed:第一次读取的时候,先把这个图片放到缓存里,下次再使用到这个同名图片的时候直接从缓存中读取;优点:方便快捷,只有第一次使用的时候稍慢,接下来再使用就会稍微快点;缺点:如果在当前工程中只使用一次会浪费内存
        //UIImage *image = [UIImage imageNamed:@"v_red_heart_selected"];  //这种初始化方式可以直接不给出图片的具体的名字,它会自己进行匹配
        
        
        //使用initWithContentsOfFile初始化图片的时候,每次都会根据路径去读取,不会占用内存,如果图片在当前工程中只使用一次,应该选择这个方法
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"v_red_heart_selected@3x.png" ofType:nil];  //这种必须拼接图片的全名称,否则image的路径为空
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
        
        
        
        /*
         123.png
         123@2x.png
         123@3x.png
         
         图片的适配相关的内容
         */
        
        //将image类型的对象转换成NSData类型的数据进行存储
        //使用UIImageJPEGRepresentation将图片转换成NSData类型的
        //第一个参数:要转换的image对象
        //第二个参数:表示图片压缩的值
        //iPhone中将大于2M的图片,会自动旋转90度,所以最终会将图片保存成jpeg格式的
        NSData *imageData = UIImageJPEGRepresentation(image, 1);
        
        //找到路径进行存储
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        //最终路径
        NSString *imagePath = [documentPath stringByAppendingString:@"/123.jpeg"];
        
        [imageData writeToFile:imagePath atomically:YES];
        
        NSLog(@"imagePath = %@",imagePath);
        
        //读取NSData类型的数据
        //需求:将NSData类型的数据读取出来,转换成UIImage类型并显示在imageView上
        NSData *newData = [NSData dataWithContentsOfFile:imagePath];
        
        UIImage *showImage = [[UIImage alloc] initWithData:newData];
        UIImageView *showImageView = [[UIImageView alloc] initWithImage:showImage];
        [self.view addSubview:showImageView];

复杂对象的写入与读取

posted on 2016-05-21 10:12  哦嘿嘿  阅读(199)  评论(0编辑  收藏  举报