-
沙盒機制:是一種安全體系的表現,我們開發的每一個應用程序,都會有一個對應的沙盒文件夾,當前程序只能在自己的沙盒文件中讀取文件,不能訪問其他應用程序的沙盒,一般存儲的是非代碼的資源(例如:圖片,視頻,聲音).....iOS8之后開辟一些空間可以供其他應用程序訪問.
在程序中動態生成的或者是從網絡上請求下來的資源,如果存儲的話,也是要存儲在沙盒文件中.NSString *homePath = NSHomeDirectory();
沙盒文件1:Documents
Document文件作用:蘋果建議將程序中建立的或者在程序中瀏覽的文件數據保存在該目錄下,itunes備份和回復的時候也會包含此目錄
- 三個參數:
*
* @param NSDocumentDirectory 它是一個枚舉值,枚舉你具體要查看的文件夾[要進入哪個文件夾直接修改其枚舉值就可以]
* @param NSUserDomainMask 表示用戶主目錄
* @param YES 一般設置為YES表示展示完整路徑
*
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
沙盒文件2:library
-
- 存儲程序默認設置,或者是其他狀態信息,里邊包含兩個文件夾caches,Preferences
- caches:緩存文件,會將app緩存的一些數據,例如音頻,視頻等文件存放其中,(不會被itunes同步)
- Preferences:偏好設置文件夾,會將app的偏好設置存放在這個文件夾中,比如說:是否允許訪問圖片,是否允許訪問地理位置......
NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
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
- tmp臨時文件夾,里面放著的是app獲取到的一些臨時文件夾.例如:當需要展示圖片時,在臨時文件中先過濾,一旦展示到界面時,就會將臨時文件中的數據清除. 關閉程序時,會自動清除臨時文件中的數據.
- 由于是臨時文件,所以不會被itunes同步備份.
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類型的數據存儲到本地(以圖片為例)
- 常用兩種初始化image的方式:
- 1:使用imageNamed:第一次讀取的時候,會先把這個圖片放到緩存當中,下次在使用到這個同名的圖片時,直接從緩存中讀取.
有點:方便快捷,只有第一次使用的時候稍慢,接下來在使用就會快些.
缺點:如果在當前工程中大量會大量的浪費內存空間
UIImage *image = [UIImage imageNamed:@"v_red_heart_selected@3x"];//這種初始化方法可以直接不給出圖片的具體名字,它會自動匹配
- 2:使用initWithContentsOfFile:初始化圖片時,每次都會根據路徑去讀取,不會占用內存,如果圖片在當前工程中只使用一次,應該選擇這個方法.
#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];