美文网首页
沙盒缓存

沙盒缓存

作者: 爱码师 | 来源:发表于2017-07-19 10:18 被阅读12次

    沙盒介绍

    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];

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

    相关文章

      网友评论

          本文标题:沙盒缓存

          本文链接:https://www.haomeiwen.com/subject/xaakkxtx.html