转载

【Swift】沙盒缓存

本地sandbox缓存目录

【Swift】沙盒缓存

沙盒の 主目录:

po NSHomeDirectory()

/Users/SpongeBob/Library/Developer/CoreSimulator/Devices/5A46B8A4-F547-4B6A-B5B8-F76A6E13998F/data/Containers/Data/Application/02DF6B84-32DF-4082-A7AE-EF4D9A110E42

沙盒の bundle(*.app包) 路径:

po [[NSBundle mainBundle] bundlePath]

/Users/SpongeBob/Library/Developer/CoreSimulator/Devices/5A46B8A4-F547-4B6A-B5B8-F76A6E13998F/data/Containers/Bundle/Application/927AB12D-9922-4B00-AAB5-2228FD4025A3/01-BundlePath&HomeDirectory.app

沙盒の Tmp 路径

po NSTemporaryDirectory()

/Users/SpongeBob/Library/Developer/CoreSimulator/Devices/5A46B8A4-F547-4B6A-B5B8-F76A6E13998F/data/Containers/Data/Application/02DF6B84-32DF-4082-A7AE-EF4D9A110E42/tmp/

沙盒の document 路径

NSLog(@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]);

/Users/SpongeBob/Library/Developer/CoreSimulator/Devices/5A46B8A4-F547-4B6A-B5B8-F76A6E13998F/data/Containers/Data/Application/21231E28-ED5C-4D6E-AE03-A981DDD33CCC/Documents

注释:

5A46B8A4-F547-4B6A-B5B8-F76A6E13998F (模拟器)设备的UUID --不可改变

02DF6B84-32DF-4082-A7AE-EF4D9A110E42 (应用程序)*.app 的UUID --随机生成

Objc:

【Swift】沙盒缓存

【Swift】沙盒缓存

 Swift:

0、变量的声明 & Swift中延迟的加载

@IBOutlet weak var imgView: UIImageView!  var str = "https://www.baidu.com/img/bdlogo.png"  // 在Swift中,只需要一行搞定“懒加载”  // 参考: http://swiftist.org/topics/129  lazy var imgCache = NSMutableDictionary() // lazy var cache : NSMutableDictionary = { //  let dic = NSMutableDictionary() //   //  return dic // }()  // MARK: lazy load array   ??? // lazy var name: String = "Spongebob" // var arr: NSArray { //  get { //   return NSArray() //  } // } 

1、网络下载图片,缓存到本地沙盒

/// 将网络图片缓存到本地沙盒     func downloadPicToCache() {  println("网络下载图片")  var urlPic = NSURL(string: str)  var data = NSData(contentsOfURL: urlPic!)  var image = UIImage(data: data!)  self.imgView.image = image  // 写入沙盒  if data != nil {      println((str as NSString).appendCacheDir())      data?.writeToFile((str as NSString).appendCacheDir(), atomically: true);  }     } 

2、加载图片

/// 加载网络图片  func loadImg() {   var img = UIImage(contentsOfFile: (str as NSString).appendCacheDir())   if self.imgCache[str] != nil {    println("从内存中加载图片")    self.imgView.image = self.imgCache[str] as? UIImage   } else {    if img != nil {     println("从沙盒(磁盘)加载图片")     println((str as NSString).appendCacheDir())     // 将图片添加到图片缓冲池中     self.imgCache.setObject(img!, forKey: str)     self.imgView.image = img    } else {     downloadPicToCache()    }   }  } 

3、这里为NSString写了一个分类(为了拼接沙盒路径地址)

/* ///  拼接文档目录 - (NSString *)appendDocumentDir; ///  拼接缓存目录 - (NSString *)appendCacheDir; ///  拼接临时目录 - (NSString *)appendTmpDir; */ - (NSString *)appendDocumentDir {  NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  // stringByAppendingPathComponent拼接时,会自动加上反斜杠"/"  // self为字符串本身,lastPathComponent获取文件路径最后的字符串!  return [dir stringByAppendingPathComponent:self.lastPathComponent]; } - (NSString *)appendCacheDir {  NSString *dir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];  return [dir stringByAppendingPathComponent:self.lastPathComponent]; } - (NSString *)appendTmpDir {  return [NSTemporaryDirectory() stringByAppendingPathComponent:self.lastPathComponent]; } 

源码分享: https://github.com/SpongeBob-GitHub/Sandbox_Caches

Swift懒加载 ,参考: http://swiftist.org/topics/129

正文到此结束
Loading...