美文网首页
plist 与沙盒

plist 与沙盒

作者: Fade1992 | 来源:发表于2019-12-17 10:35 被阅读0次

读取plist文件中的数据

        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil];
        
        //根据plist文件中的数据类型分别赋值给字典或数组
//        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:filePath];
        NSMutableArray *appArray = [NSMutableArray array];
        for (NSDictionary *dic in dictArr) {
            [appArray addObject:[XMGApp appWithDict:dic]];
        }
        _apps = appArray;

将网络图片下载放入沙盒中对应的路径中和使用数据案例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"app";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    XMGApp *app = self.apps[indexPath.row];
    cell.textLabel.text = app.name;
    cell.detailTextLabel.text = app.download;
    
    // 先从内存缓存中取出图片
    UIImage *image = self.imageCache[app.icon];
    if (image) { // 内存中有图片
        cell.imageView.image = image;
    } else {  // 内存中没有图片
        // 获得Library/Caches文件夹
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

        NSString *sandboxPath = NSHomeDirectory();

        NSString *userNameStr = NSUserName();


        // 获得文件名
        NSString *filename = [app.icon lastPathComponent];
        // 计算出文件的全路径
        NSString *file = [cachesPath stringByAppendingPathComponent:filename];
        // 加载沙盒的文件数据
        NSData *data = [NSData dataWithContentsOfFile:file];
        
        if (data) { // 直接利用沙盒中图片
            cell.imageView.image = [UIImage imageWithData:data];
            // 存到字典中
            self.imageCache[app.icon] = cell.imageView.image;
        } else { // 下载图片
            data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
            cell.imageView.image = [UIImage imageWithData:data];
            
            // 存到字典中
            self.imageCache[app.icon] = cell.imageView.image;
            // 将图片文件数据写入沙盒中
            [data writeToFile:file atomically:YES];

        }
    }
    
    return cell;
}

拓展

//    [NSDate date];
//    获得文件属性
//    [[NSFileManager defaultManager] attributesOfItemAtPath:<#(NSString *)#> error:<#(NSError *__autoreleasing *)#>];
//    删除文件
//    [[NSFileManager defaultManager] removeItemAtPath:<#(NSString *)#> error:<#(NSError *__autoreleasing *)#>];

//沙盒中的文件夹
/*
 Documents :用户手机连接iTunes 或 iCloud 会把数据备份到苹果的服务器 (不建议存放下载的数据会审核不通过)
 Library
    - Caches:下载的数据永远存在 除非卸载
    - Preference:
 tmp :临时文件夹(随时会被系统随机的清理掉)
 */

//内存缓存的字典保存图片用
/*
 一个key:http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fbb.png
 value: <UIImage: 0x6040002a2640>, {48, 48}
 {
 "http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fbb.png" = "<UIImage: 0x6040002a2640>, {48, 48}";
 "http://p19.qhimg.com/dr/48_48_/t0101e2931181bb540d.png" = "<UIImage: 0x6040002a4e60>, {48, 48}";
 }
 */

相关文章

网友评论

      本文标题:plist 与沙盒

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