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;