沙盒缓存

沙盒介绍

1. 查找沙盒路径

a.  沙盒路径   po NSHomeDirectory()
b.  bundle路径   po [[NSBundle mainBundle] bundlePath] 

2. 沙盒目录

### Documents

保存由应用程序产生的文件或数据。例如:游戏进度、涂鸦软件的绘图
目录中的文件会自动保存到iCloud上
不要保存从网络上下来的文件
iTunes会备份
### Library/Cache
保存临时文件,后续需要使用。例如:缓存图片,离线地图数据
系统不会自动清理此目录。
程序员需要提供清理此目录的功能
iTunes不会备份
### Library/Preferences
用户偏好,存储用户的一些偏好操作
iTunes会备份
### tmp
保存临时文件,后续不需要使用
tmp目录中的文件,系统会自动清理
系统的磁盘空间不足,会自动清理
系统重启,会清理该文件夹
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];
    }

//把数据写入沙盒中
[data writeToFile:self.URLString.appendCachePath atomically:YES];

在判断完内存缓存后,如果没有,则从沙盒加载图片,虽然速度“慢点”,但是不浪费流量,省钱啊!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容