iOS沙盒机制

1、沙盒

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件

Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录,如果保存了下载的数据,程序提交会被直接被拒绝

Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

Library/Preferences:偏好设置文件

tmp:提供一个即时创建临时文件的地方,在iPhone在重启时,会丢弃所有的tmp文件。

在模拟器上,沙盒目录是变化的,所以每次都要打印
NSString * path1 = NSHomeDirectory();
NSLog(@"%@",path1);

偏好设置
偏好设置,用户信息,是否推送,是否支持3G.
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
//存储偏好数据
    [ud setObject:@"11" forKey:@"age"];
    
    [ud setInteger:20 forKey:@"weight"];
    
    //    setObject + synchronize
    //马上存入本地
    [ud synchronize];
    
//从本地获取
    NSLog(@"%@", [ud objectForKey:@"username"]);
    [ud removeObjectForKey:@"age”];

2、NSCache

//系统用来缓存的类
NSCache * cache = [[NSCache alloc] init];

//totalCostLimit 设置缓存大小

//设置缓存数量,清除的是先缓存的
cache.countLimit = 5;

//cache.delegate = self;

for (NSInteger i = 0; i < 20; i ++) {
    [cache setObject:[NSString stringWithFormat:@"cache - %@",@(i)] forKey:@(i)];
}

for (NSInteger i = 0; i < 20; i ++) {
    NSLog(@"%@",[cache objectForKey:@(i)]);
}

//谁不在缓存中
- (void)cache:(NSCache *)cache willEvictObject:(id)obj {

    NSLog(@"%@",obj);
    }

3、SDWebImage

    //网络图片的URL
//    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:urlString]];
    
    //占位图
//    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
    
    //完成block
//    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defaultImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        
//        if (error) {
//            NSLog(@"下载失败");
//        } else {
//            
//            NSLog(@"image - %@",image);
//            
//            NSLog(@"cacheType - %@",@(cacheType));
//        }
//        
//    }];
    
    /*
     options所有选项:
     
     //失败后重试 (经常使用)
     SDWebImageRetryFailed = 1 << 0,
           
     //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。(经常使用)
     SDWebImageLowPriority = 1 << 1,
           
     //只进行内存缓存
     SDWebImageCacheMemoryOnly = 1 << 2,
           
     //这个标志可以渐进式下载,显示的图像是逐步在下载
     SDWebImageProgressiveDownload = 1 << 3,
           
     //刷新缓存
     SDWebImageRefreshCached = 1 << 4,
           
     //后台下载
     SDWebImageContinueInBackground = 1 << 5,
           
     //优先下载
     SDWebImageHighPriority = 1 << 8,
           
     //延迟占位符
     SDWebImageDelayPlaceholder = 1 << 9,
           
     //改变动画形象
     SDWebImageTransformAnimatedImage = 1 << 10,
     
     */
    
//    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:SDWebImageRetryFailed|SDWebImageLowPriority];
    
    //下载图片监测进度
    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        //receivedSize  已接受到的大小
        //expectedSize  期待的大小  总大小
        
//        NSLog(@"receivedSize - %@",@(receivedSize));
//        NSLog(@"expectedSize - %@",@(expectedSize));

        
         CGFloat progress  = (float)receivedSize/expectedSize;
        
        NSLog(@"%g",progress);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"图片加载完成");
    }];

你可能感兴趣的:(iOS沙盒机制)