源码0502-06-掌握-多图片下载

//  ViewController.m
//  06-掌握-多图片下载
#import "ViewController.h"
#import "XMGApp.h"

@interface ViewController ()
/** 所有数据 */
@property (nonatomic, strong) NSArray *apps;

/** 内存缓存的图片 */
@property (nonatomic, strong) NSMutableDictionary *imageCache;
@end

@implementation ViewController

- (NSMutableDictionary *)imageCache
{
    if (!_imageCache) {
        _imageCache = [NSMutableDictionary dictionary];
    }
    return _imageCache;
}

- (NSArray *)apps
{
    if (!_apps) {
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];

        NSMutableArray *appArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            [appArray addObject:[XMGApp appWithDict:dict]];
        }
        _apps = appArray;
    }
    return _apps;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.apps.count;
}

- (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 *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
 Library
    - Caches
    - Preference
 tmp
 */
@end

//  XMGApp.h
//  06-掌握-多图片下载
#import <Foundation/Foundation.h>

@interface XMGApp : NSObject
/** 图标 */
@property (nonatomic, strong) NSString *icon;
/** 下载量 */
@property (nonatomic, strong) NSString *download;
/** 名字 */
@property (nonatomic, strong) NSString *name;

+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
//  XMGApp.m
//  06-掌握-多图片下载
#import "XMGApp.h"

@implementation XMGApp
+ (instancetype)appWithDict:(NSDictionary *)dict
{
    XMGApp *app = [[self alloc] init];
    [app setValuesForKeysWithDictionary:dict];
    return app;
}
@end
//  ViewController.m
//  06-掌握-多图片下载
#import "ViewController.h"
#import "XMGApp.h"

@interface ViewController ()
/** 所有数据 */
@property (nonatomic, strong) NSArray *apps;

/** 内存缓存的图片 */
@property (nonatomic, strong) NSMutableDictionary *images;

/** 队列对象 */
@property (nonatomic, strong) NSOperationQueue *queue;
@end

@implementation ViewController

- (NSOperationQueue *)queue
{
    if (!_queue) {
        _queue = [[NSOperationQueue alloc] init];
        _queue.maxConcurrentOperationCount = 3;
    }
    return _queue;
}

- (NSMutableDictionary *)images
{
    if (!_images) {
        _images = [NSMutableDictionary dictionary];
    }
    return _images;
}

- (NSArray *)apps
{
    if (!_apps) {
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];

        NSMutableArray *appArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            [appArray addObject:[XMGApp appWithDict:dict]];
        }
        _apps = appArray;
    }
    return _apps;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.apps.count;
}

- (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.images[app.icon];
    if (image) { // 内存中有图片
        cell.imageView.image = image;
    } else {  // 内存中没有图片
        // 获得Library/Caches文件夹
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        // 获得文件名
        NSString *filename = [app.icon lastPathComponent];
        // 计算出文件的全路径
        NSString *file = [cachesPath stringByAppendingPathComponent:filename];
        // 加载沙盒的文件数据
        NSData *data = [NSData dataWithContentsOfFile:file];

        if (data) { // 直接利用沙盒中图片
            UIImage *image = [UIImage imageWithData:data];
            cell.imageView.image = image;
            // 存到字典中
            self.images[app.icon] = image;
        } else { // 下载图片
            [self.queue addOperationWithBlock:^{
                // 下载图片
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
                UIImage *image = [UIImage imageWithData:data];

                [NSThread sleepForTimeInterval:1.0];

                // 回到主线程显示图片
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    cell.imageView.image = image;
                }];

                // 存到字典中
                self.images[app.icon] = 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
 Library
    - Caches
    - Preference
 tmp
 */
@end
时间: 2024-08-10 09:15:42

源码0502-06-掌握-多图片下载的相关文章

2014年最新720多套Android源码2.0GB免费一次性打包下载

之前发过一个帖子,但是那个帖子有点问题我就重新发一个吧,下面的源码是我从今年3月份开始不断整理源码区和其他网站上的android源码,目前总共有720套左右,根据实现的功能被我分成了100多个类,总共2G多,还在不断更新安卓源码.初学者可以快速方便的找到自己想要的例子,大神也可以看一下别人的方法实现.虽然的例子都是我一个人辛辛苦苦花了很多时间和精力整理的,但是既然这些例子是来自于社区那就让他们免费回归社区吧,(是的!特么的不要一分钱!最看不起那些挂羊头卖狗的)你可以在本帖里面按Ctrl+F查找你

.NET源码保护控件VMProtect免费下载及使用教程脱壳等功能详解

原文来自VMProtect龙博方案网www.fanganwang.com VMProtect是一款全新的软件保护工具.与其它大部分的保护程序不同,VMProtect可修改程序的源代码.VMProtect可将被保护文件中的部分代码转化到在虚拟机(以下称作VM)上运行的程序(以下称作bytecode)中.您同样可把VM想象为具备命令系统的虚拟处理器,该命令系统与Intel 8086处理器所使用的完全不同.例如,VM没有负责比较2个操作数的命令,也没有有条件与无条件的移转等.就象您现在看到的,黑客必须

Spark MLlib机器学习算法、源码及实战讲解pdf电子版下载

Spark MLlib机器学习算法.源码及实战讲解pdf电子版下载 链接:https://pan.baidu.com/s/1ruX9inG5ttOe_5lhpK_LQg 提取码:idcb <Spark MLlib机器学习:算法.源码及实战详解>书中讲解由浅入深慢慢深入,解析讲解了MLlib的底层原理:数据操作及矩阵向量计算操作,该部分是MLlib实现的基础:并对此延伸机器学习的算法,循序渐进的讲解其中的原理,是读者一点一点的理解和掌握书中的知识. 目录 · · · · · · 第一部分 Spa

转--2014年最新810多套android源码2.46GB免费一次性打包下载

转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源码,目前总共有810套左右,根据实现的功能被博主分成了100多个类,总共接近2.5G,还在不断更新.初学者可以快速方便的找到自己想要的例子,大神也可以看一下别人的方法实现.虽然的例子都是博主一个人辛辛苦苦花了很多时间和精力整理的,但是既然这些例子是来自于社区那就让他们免费回归社区吧,(是的!特么的不

Android应用源码可以按音乐视频图片分类浏览的安卓文件浏览器

本项目是一个可按音乐.图片.视频分类浏览的安卓文件浏览器.具有多选.打开.复制.粘贴.删除.重命名.查看属性等功能,并且还可以切换列表显示效果和对文件分类浏览,比较遗憾的是源码注释不多,项目编码UTF-8编译版本2.3.3.MediaCenter是程序的主项目依赖moduleAboutus和moduleAnimtab两个项目,三个项目如果导入以后没有自动添加引用关系需要手动在Library里面添加引用关系. 代码截图: 运行效果截图: 源码包免费下载地址:[点击这里]

[c#源码分享]客户端程序传送图片到服务器

源码 (因为空间大小限制,不包含通信框架源码,通信框架源码请另行下载) 以前帮朋友做了一个图片采集系统,客户端采集相片后,通过TCP通信传送到服务器,本文把客户端传送图片到服务器的这部分提取出来. 由于每张图片的大小都不大,所以我们在传输图片时,没有采用传送文件的方式,而是采用了直接序列化图片的方式来进行. 当前支持的图片类型: jpg,png,gif 您可以自己添加扩充支持的图片类型 通信框架采用英国的开源的networkcomms2.3.1 通信框架   序列化器采用开源的protobuf.

Spring AMQP 源码分析 06 - 手动消息确认

### 准备 ## 目标 了解 Spring AMQP 如何手动确认消息已成功消费 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListener> ## 相关资源 Offical doc:<http://docs.spring.io/spring-amqp/docs/1.7.3.RELEASE/reference/html/_reference.html#message-listener-adapter> Sample code:<https:

非常适合新手的jq/zepto源码分析06 -- 事件模型

复习下事件的有关内容: 1.现在用的绑定/删除: obj.addEventListener(type,fn,false) obj.removeEventListener(type) obj.attachEvent(type,fn)   //ie obj.detachEvent(type) 2.js的event对象 type : 事件类型 srcElement/target :  事件源 button : 如果是鼠标按下,则 1左键 2右键 4中间滚轮 多个键则相加按下的所有值. firfox 0

从源码编译UE4,加快Setup.bat下载文件的环节

之前很傻,每次运行这个setup.bat都要等很久很久才能把4g多的东西下载完成,知道有一天突然发现了世外桃源-- 从命令行运行setup.bat -help,可以看到参数的说明(没错,参数可选,之前一直以为bat都是拿来双击的--): Usage: GitDependencies [options] Options: --all Sync all folders --include=<X> Include binaries in folders called <X> --excl

SDWebImage 源码分析 --加载gif图片

n年关了,马上放假,终于把手头上的事情告一段落,连续发布了3个app,我也是醉了. 终于有了点时间.想研究下SDWebImage是怎么加载gif图片的. 一直很好奇. 现在开始. 1,首先我们看下SDWebImage是怎么加载gif的. faceButton.image = [UIImage sd_animatedGIFNamed:[NSString stringWithFormat:@"CHATA_%d",i - 46]]; sd_animatedGIFNamed是SDWebImag