IOS文件系统
当开发者第一次启动app时,IOS操作系统就为此app创建了一个文件系统,该文件系统下默认有四个,分别是:
Documents:存储用户在操作app时产生的数据,使目录下的数据可以通过iCloud进行同步。
Library:用户偏好设置数据,通常和此类NSUserDefaults搭配使用,此目录下的数据可以通过iCloud进行同步。
Tmp:存在临时数据,此目录下的数据不会通过iCloud进行同步。
Ipa包:开发者不会操作此目录,通常是通过此类NSBundle类。
//获取程序根目录
NSString *homePath = NSHomeDirectory();
//获取根目录下的Documents目录
NSString *documentsPath = [homePath stringByAppendingPathComponent:@"Documents"];
//或者
documentsPath = [homePath stringByAppendingFormat:@"/Documents"];
//最常用的获取Documents目录
documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"documentPath = %@",documentsPath);
//字符串拼接
NSString *libraryPath = [homePath stringByAppendingString:@”Documents”];
//C函数获取Library目录
libraryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
libraryPath = [homePath stringByAppendingString:@"/library"];
NSLog(@"libraryPath = %@",libraryPath);
//获取tmp目录
NSString *tmpPath = NSTemporaryDirectory();
//app包,获取包内的图片,显示在UI上
NSBundle *bundle = [NSBundle mainBundle];
NSLog(@"bundlePath = %@",bundle.bundlePath);
NSString *imgPath = [bundle pathForResource:@"5.pic" ofType:@"jpg"];
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
UIImage *image = [UIImage imageWithData:imgData];
self.imageView.image = image;