沙盒机制、NSBundle、简单对象写入文件

1、沙盒机制:沙盒的本质就是一个文件夹,名字是随机的。

2.沙盒的构成

Document:通常用来存放应用程序需要持久化使用的关键数据,比如:本地数据库等、iTunes在备份的时候会自动备份此文件夹

Library:通常用来存储应用程序运行期间生成的持久化数据,比如:用户账号和账户名等。应用程序退出后不会被删除文件夹内的数据,但iTunes在备份的时候不会自动备份此文件夹。

tmp:用来存放临时文件,应用程序结束后清除。

Library/caches:运行期间的缓存文件,历史记录等。

Library/preferences:应用程序的设置:如夜间模式等

.app:程序包,不存在沙盒中,在bundle中

3.用代码获得沙盒的路径

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);

//注意:

(1).该方法返回的是一个数组,在mac上开发数组中有多个对象,而在iOS中开发中该数组中只有一个对象

(2).第一个参数:要查找的文件夹的名

第二个参数:是一个枚举类型,但在iOS中开发只用:NSUserDomainMask

第三个参数:BOOL类型,YES:绝对路径;NO:相对路径

(3).获取沙盒目录路径的方法

NSHomeDirectory()--------------沙盒主路径

NSDocumentDirectory-------------Documents文件路径

NSLibraryDirectory--------------Library文件路径

NSCachesDirectory------------Caches文件路径

NSTemporaryDirectory()-------tmp文件路径

获得沙盒各个文件夹路径的代码:

//获取沙盒主路径

//    NSString *homePath = NSHomeDirectory();
//    NSLog(@"%@",homePath);


//    //获得document路径(document:通常用来存放应用程序需要持久化使用的关键数据,比如本地数据库等、iTunes在备份的时候会自动备份此文件夹)
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,NO) objectAtIndex:0];
//    NSLog(@"%@",documentPath);//此时输出的是相对路径
//    
//    
//    //获得library路径(library:通常用来存储应用程序运行期间生成的持久化数据,比如:用户账户名等。应用程序退出后不会被删除文件夹内数据,但是iTunes备份时不会备份此文件夹)
//    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    
//    NSLog(@"%@",libraryPath);
//    
//    
//    //获得tmp路径(tmp:用来存放临时文件,应用程序结束后清除)
//    NSString *tempPath = NSTemporaryDirectory();
//    NSLog(@"%@",tempPath);
//    
//    //获得cache路径(cache:运行期间的缓存文件,历史记录等)
//    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSLog(@"%@",cachePath);


4.NSBundle:bundle是一个目录,其中包含了程序会使用到的资源.这些资源包含了如图像,声音,编译好的代码,nib文件,配置文件、app文件等

//    //获取当前应用程序包
//    NSBundle *bundle = [NSBundle mainBundle];
//    
//    //获取当前包的路径(方法一)
//    //NSString *bundlePath = [bundle resourcePath];
//    
//    //或者(方法二)
//    NSString *bundlePath = [bundle bundlePath];
//    NSLog(@"%@",bundlePath);
//    
//    NSString *imgPath = [bundle pathForResource:@"image4" ofType:@"jpg"];
//    NSLog(@"%@",imgPath);
//    
//    NSString *interfacePath = [bundle pathForResource:@"蓝鸥接口文档" ofType:@"txt"];
//    
//    NSLog(@"%@",interfacePath);


5.简单对象写入文件

(1).简单对象:NSString、NSSArray、NSDictionary、NSData(注意:当要讲图片存入文件中,则要先转换成NSData类型)


    //字符串写入文件
    
    NSString *incantation = @"等闲变却故人心,却道故人心易变";
    
    //创建一个路径
    NSString *path = NSHomeDirectory();

   //用该方法:要加“/和类型(.txt)”

    path = [path stringByAppendingString:@"/纳兰容若.txt"];
    
    //第二个参数是文件保护作用
    
    //将字符串写入文件
    [incantation writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
    //从文件中读取
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",string);
    
    
    //数组写入文件
    
    //创建路径
    NSString *homePath = NSHomeDirectory();

//用该方法不用加"/"和类型(如.txt)

    homePath = [homePath stringByAppendingPathComponent:@"王安石"];
    
    //写入数组
    NSArray *array = @[@"相怜相恋倍相亲",@"一生一世一双人"];
    [array writeToFile:homePath atomically:YES];
    
    
    
    //读取部分
    NSArray *arrayString = [NSArray arrayWithContentsOfFile:homePath];
    
    NSLog(@"%@",arrayString[0]);
    
    
    
   //字典写入文件
    
    NSString *dictPath = NSHomeDirectory();
    dictPath = [dictPath stringByAppendingPathComponent:@"南方不败"];
    NSDictionary *dict = @{@"周哥":@"宁可我负天下人,不可天下人负我"};
    
    [dict writeToFile:dictPath atomically:YES];
    
    
    //字典读取文件
    NSDictionary *dictString = [NSDictionary dictionaryWithContentsOfFile:dictPath];
    NSLog(@"%@",dictString[@"周哥"]);
    
    
    //存储图片
    UIImage *image = [UIImage imageNamed:@"1.jpg"];

   JPEG的转换方法里面第二个参数是压缩系数(在0~1)之间

    NSData *data = UIImageJPEGRepresentation(image, 1);
    
    NSString *pathPic = NSHomeDirectory();
    pathPic = [pathPic stringByAppendingString:@"/pic.jpg"];
    //存图片
    [data writeToFile:pathPic atomically:YES];
    
    
    //取图片
    NSData *dataPic = [NSData dataWithContentsOfFile:pathPic];
    UIImage *image1 = [UIImage imageWithData:dataPic];
    NSLog(@"%@",image1);
 





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值