码迷,mamicode.com
首页 > 编程语言 > 详细

【Swift】沙盒缓存

时间:2015-08-06 14:44:13      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

本地sandbox缓存目录
 
技术分享
 
沙盒の 主目录:
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:
 
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

【Swift】沙盒缓存

标签:

原文地址:http://www.cnblogs.com/SpongeBob-GitHub/p/4707786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!