iOS 沙盒机制

2016-05-23  本文已影响145人  唐半仙丶

沙盒文件1:Documents

Document文件作用:苹果建议将程序中建立的或者在程序中浏览的文件数据保存在该目录下,itunes备份和回复的时候也会包含此目录

  • 三个参数:
 *
 *  @param NSDocumentDirectory 它是一个枚举值,枚举你具体要查看的文件夹[要进入哪个文件夹直接修改其枚举值就可以]
 *  @param NSUserDomainMask    表示用户主目录
 *  @param YES                 一般设置为YES表示展示完整路径
 *
 

 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

沙盒文件2:library

2.1:获取Library/Caches目录

NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",paths);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];

2.2:获取Library/Preferences目录

    NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
   //stringByAppendingPathComponent:就是将前面路径格式和后面的普通字符串格式连接在一起,并且以路径格式返回.
NSString *path1 = [[path2 objectAtIndex:0] stringByAppendingPathComponent:@"Preferences"];
 NSLog(@"----%@",path1);

沙盒文件3:tmp

 NSString *tmpPath = NSTemporaryDirectory();
 NSLog(@"%@",tmpPath);
     //获取xxxx.app文件地址
    NSString *appPath = [[NSBundle  mainBundle] resourcePath];
    NSLog(@"%@",appPath);

将NSArray类型的数据存储到本地

#pragma mark ---  将NSArray类型的数据存储到本地
    //1:找到Documents文件夹
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    //2:创建需要存储的数组
    NSArray *array = @[@"小明",@"小丽",@"小花",@"小强",@"小红"];
    //3:创建数组存储的最终路径
    NSString *arrayPath = [documentsPath stringByAppendingPathComponent:@"BJS.plist"];
    //4:写入本地
    [array writeToFile:arrayPath atomically:YES];
    
    NSLog(@"arrayPath = %@",arrayPath);
    
    //将存在本地的数据取出
    NSArray *newArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"newArray = %@",newArray);
    

将字典类型的数据存储到本地

#pragma mark ----  将字典类型的数据存储到本地
    //找到文件路径
    NSString *documentPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //创建存储字典的文件夹
    NSDictionary *dictionary = @{@"name":@"小丽",@"gender":@"未知",@"age":@"18"};
    //最终存入的路径
    NSString *dicPath = [documentPath stringByAppendingString:@"/wj.plist"];
    //写入:
    [dictionary writeToFile:dicPath atomically:YES];
    NSLog(@"dicPath = %@",dicPath);
    
    //读取:
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"dic = %@",dic);

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

  • 1:使用imageNamed:第一次读取的时候,会先把这个图片放到缓存当中,下次在使用到这个同名的图片时,直接从缓存中读取.
    有点:方便快捷,只有第一次使用的时候稍慢,接下来在使用就会快些.
    缺点:如果在当前工程中大量会大量的浪费内存空间
    UIImage *image = [UIImage imageNamed:@"v_red_heart_selected@3x"];//这种初始化方法可以直接不给出图片的具体名字,它会自动匹配
#pragma  mark ---  将NSData类型的数据存储到本地(以图片为例)
   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"v_red_heart_selected@3x.png" ofType:nil];//这种必须拼接图片的全名称,否则image路径为空
   UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
   
   //将image类型的对象转换成为NSData类型数据进行存储
   //使用UIImageJPEGRepresentation:将图片转换成为NSData类型
   //第一个参数:要转换的image对象
   //第二个参数:表示图片压缩的值
   //IPhone中将大于2M的图片,使用该方法,最终会将图片保存成jpeg格式
   NSData *imageData = UIImageJPEGRepresentation(image, 1);
   //找到路径存储
   NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   //最终路径
   NSString *imagePath = [documentPath stringByAppendingString:@"/1234.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];
上一篇 下一篇

猜你喜欢

热点阅读