OC学习日记016-IOS文件系统
来源:互联网 发布:数据抓取系统 编辑:程序博客网 时间:2024/06/06 12:43
IOS文件系统
前言
我们其实已经把OC的大致内容学完了,我们在上几章已经开始用iOS的结构去写程序了,我们今天来讲讲IOS的文件结构,以便于我们之后讲UI的时候可以更好的接轨。
IOS的文件系统组成
当第一次启动app 的时候,IOS操作系统就为此APP创建一个文件系统,该文件系统下默认有四个目录,分别是:
Documents:存储用户在操作app时产生的数据,此目录下的数据可以通过iCloud进行同步 Library:用户偏好设置,通常和此类NSUserDefaults搭配使用,在此目录下的数据可以通过iCloud进行同步 tmp:存放临时数据,在此目录下的数据不会通过iCloud同步 app包:开发者不会操作此目录,通常是通过NSBundle来获取包内资源,如工程的素材
访问方法
在新的IOS文件系统结构中,app包的存储路径跟Documents、Library、tmp不是在同一个文件夹下,所以app包的访问方法与其他三个有所不同:
app包
NSLog(@"%@",[NSBundle mainBundle]);
获取程序的根目录
NSString *rootPath= NSHomeDirectory();
而其他三个文件夹都是在同一个目录下,所以它们的访问方法都是大同小异(tmp有它独有的方法),我们要先找到它们的根目录。
想要获取根目录下Documents
NSString *documentsPath= [rootPath stringByAppendingPathComponent:@"Documents"];
或者
NSString *documentsPath =[rootPath stringByAppendingFormat:@"/Documents"];
最常用的获取Documents目录的方式(也是Library最常用的方式)
NSArray *array= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);documentsPath = [array objectAtIndex:0];
讲上述两行代码合并后,可以简写为:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
获取Library目录
字符串拼接方式进行获取
NSString *LibraryPath=[rootPath stringByAppendingPathComponent:@"Library"];
获取Library最常用的方式
LibraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
获取tmp目录
tmp独有的访问路径的方法
NSString *tmpString= NSTemporaryDirectory();
下载文件
下载一个视频文件到Documents目录下的Video文件
我们首先要实现一个在Documents文件夹下创建文件夹的方法:
-(NSString *)createDirInDocuments:(NSString *)dirName{ //获取Documents的文件路径 NSString *doucumentsPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //拼接成我们想要的文件的路径的字符串 NSString *dirDocuments=[doucumentsPath stringByAppendingPathComponent:dirName]; //NSFileManager 单例类,用于文件的创建 NSFileManager *fileManager =[NSFileManager defaultManager]; //判断本地是否存在某个文件或者文件夹 BOOL isExist = [fileManager fileExistsAtPath:dirDocuments]; if (!isExist) { //创建文件夹 NSError *error; BOOL isSuccess = [fileManager createDirectoryAtPath:dirDocuments withIntermediateDirectories:YES attributes:nil error:&error]; if(!isSuccess){ //如果文件夹创建文件夹,将错误信息打印 NSLog(@"error= %@",error.debugDescription); dirDocuments=nil; } } return dirDocuments;}
然后我们借助这个方法去创建Video文件夹,然后判断此路径是否已经有这个文件,再借助data类型进行下载:
NSString *videoPath=[self createDirInDocuments:@"Video"];if(videoPath != nil){ NSString *videoURLString=@"http://163.177.2.46/youku/6573B6F0A684382F3E4D0341F2/0300080100536FBEDAABC00172D4733C053123-91E7-7011-9F9B-ECC617DC9593.mp4"; NSString *filePath=[videoPath stringByAppendingPathComponent:[videoURLString lastPathComponent]]; //如果文件夹中不存在该路径 NSFileManager *fileManager=[NSFileManager defaultManager]; if(![fileManager fileExistsAtPath:filePath]){ NSData *data = nil; // NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:videoURLString ]]; //用NSFileManger的对象,将文件写入本地 BOOL isSuccess=[fileManager createFileAtPath:filePath contents:data attributes:nil]; if (isSuccess) { NSLog(@"视频下载成功"); } else{ NSLog(@"视频下载失败"); } } }
下载三张指定的图片到tmp中的Imgs文件夹中
同样我们要先实现一个在一个tmp文件夹下创建文件夹的方法
-(NSString *)createDirInTmp:(NSString *)dirName{ NSString *tmpPath=NSTemporaryDirectory(); NSString *dirPath=[tmpPath stringByAppendingPathComponent:dirName]; NSFileManager *fileManager=[NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:dirPath]) { NSError *error; BOOL isSucess=[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]; if(!isSucess){ NSLog(@"error = %@",error.debugDescription); } } return dirPath;}
然后我们对指定的图片路径建立一个数组储存,通过循环分别对它们进行下载:
NSString *imgsDocumentsPath=[self createDirInTmp:@"Imgs"]; NSArray *imgsArray=@[@"http://d.hiphotos.baidu.com/image/pic/item/6a63f6246b600c331c964cf21d4c510fd9f9a119.jpg",@"http://c.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce9c017233b1003af33a87b219.jpg",@"http://f.hiphotos.baidu.com/image/pic/item/e1fe9925bc315c60d916f9d58ab1cb134954770d.jpg"];if (imgsDocumentsPath !=nil) { for (int i=0; i<imgsArray.count; i++) { NSString *imgsString=[imgsArray[i] lastPathComponent]; //每一张图片的路径 NSString *imgsPath=[imgsDocumentsPath stringByAppendingPathComponent:imgsString]; //如果文件夹中不存在该路径 NSFileManager *fileManager=[NSFileManager defaultManager]; if(![fileManager fileExistsAtPath:imgsPath]){ //将每张图片的URL进行编码 //[imgsArray[i] stringByAddingPercentEscapesUsingEncoding:4]; NSString *urlString=[imgsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: urlString]]; if (data ==nil) { NSLog(@"网络有问题,请稍后再下载"); } else{ BOOL isSucess=[data writeToFile:imgsPath atomically:YES]; if (isSucess) { NSLog(@"图片下载成功"); } else{ NSLog(@"图片下载失败"); } } } else{ NSLog(@"图片已存在"); } }}
0 0
- OC学习日记016-IOS文件系统
- [oc学习日记]字典
- OC学习日记01
- 黑马程序员-iOS学习日记(一)OC基本语法
- 黑马程序员--IOS学习日记:01--OC概述
- 黑马程序员--IOS学习日记:02--OC类和对象
- 黑马程序员--IOS学习日记:06--OC的核心语法
- 黑马程序员--IOS学习日记:12--OC集合类
- [oc学习日记]代理模式
- OC学习日记07-数组
- OC学习日记09-字典
- OC学习日记013-协议
- 黑马程序员--IOS学习日记:03--OC面向对象的三大特性
- 黑马程序员--IOS学习日记:13--OC中的block和protocol
- IOS学习日记1
- ios学习日记
- IOS学习日记
- iOS个人学习日记
- Zookeeper: 一个分布式应用程序协调服务
- 存储引擎
- Intellij+Tomcat安装及Hello Tomcat
- 【Linux c】linux shell
- ARM cortex a 之时钟系统2
- OC学习日记016-IOS文件系统
- gdb 调试程序
- Python 读取csv的某列
- poj3608Bridge Across Islands+两凸包最近距离
- 通过api获取句柄控制其他窗体
- CEF 加载flash 插件
- UML类图中类与类的四种关系图解
- VS 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
- 不良资产处置(九):资产证券化在不良资产处置中的应用