沙盒介紹
1. 查找沙盒路徑
a. 沙盒路徑 po NSHomeDirectory()
b. bundle路徑 po [[NSBundle mainBundle] bundlePath]
2. 沙盒目錄
### Documents
保存由應用程序產(chǎn)生的文件或數(shù)據(jù)欢顷。例如:游戲進度、涂鴉軟件的繪圖
目錄中的文件會自動保存到iCloud上
不要保存從網(wǎng)絡上下來的文件
iTunes會備份
### Library/Cache
保存臨時文件箫攀,后續(xù)需要使用。例如:緩存圖片,離線地圖數(shù)據(jù)
系統(tǒng)不會自動清理此目錄涉馁。
程序員需要提供清理此目錄的功能
iTunes不會備份
### Library/Preferences
用戶偏好汪疮,存儲用戶的一些偏好操作
iTunes會備份
### tmp
保存臨時文件峭火,后續(xù)不需要使用
tmp目錄中的文件,系統(tǒng)會自動清理
系統(tǒng)的磁盤空間不足智嚷,會自動清理
系統(tǒng)重啟卖丸,會清理該文件夾
iTunes不會備份
獲取沙盒的路徑
- (instancetype)appendCachePath {
return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:self.lastPathComponent];
}
- (instancetype)appendDocumentPath {
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:self.lastPathComponent];
}
- (instancetype)appendTmpPath {
return [NSTemporaryDirectory() stringByAppendingPathComponent:self.lastPathComponent];
}
沙盒緩存圖片
先把下載的圖片以文件的形式保存下來
//獲取沙盒路徑
- (NSString *)appendCachePath {
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:self.lastPathComponent];
}
//把數(shù)據(jù)寫入沙盒中
[data writeToFile:self.URLString.appendCachePath atomically:YES];
在判斷完內(nèi)存緩存后,如果沒有盏道,則從沙盒加載圖片稍浆,雖然速度“慢點”,但是不浪費流量猜嘱,省錢靶品恪!