博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java线程实现图片_多线程实现多图片下载1
阅读量:4356 次
发布时间:2019-06-07

本文共 2735 字,大约阅读时间需要 9 分钟。

展示效果如下:

d92ab8a0443a82d00383307a15ead41b.gif

大家可以看到这个界面很简单,其实就是UITableView的布局,

但是难点是在于如何从网上下载这些图片,下载之后应如何进行存储!

我们一步一步进行解析,先从单线程(主线程)进行多图片下载

我们布局上的文字及图片的地址从plist文件中进行读取

18985c99aca077020d6b398bdba80928.png

根据结构,我们自定义一个数据模型文件

DDZApp.h

#import

@interfaceDDZApp : NSObject//图标

@property (nonatomic,strong) NSString *icon;//名字

@property (nonatomic,strong) NSString *name;//下载量

@property (nonatomic,strong) NSString *download;+ (instancetype)appWithDict:(NSDictionary *)dict;@end

DDZApp.m

#import "DDZApp.h"

@implementationDDZApp+ (instancetype)appWithDict:(NSDictionary *)dict {

DDZApp*app =[[self alloc] init];

[app setValuesForKeysWithDictionary:dict];returnapp;

}@end

以下的都是视图控制器中的代码

ViewController.m

1.

@interfaceViewController ()//所有数据

@property (nonatomic,strong)NSArray *apps;//内存缓存图片

@property (nonatomic,strong)NSMutableDictionary *imgCache;@end

第一个属性用于存储读取plist文件中的内容,设置为属性保存起来,就可以不用重复读取

第二个属性用于保存从网上下载下来的图片,也是为了不用重复读取

2.

@implementationViewController//读取数据

- (NSArray *)apps {if (!_apps) {//从plist文件中读取数据

NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist"ofType:nil]];

NSMutableArray*appArray =[NSMutableArray array];for (NSDictionary *dict indictArray) {

[appArray addObject:[DDZApp appWithDict:dict]];

}

_apps=appArray;

}return_apps;

}//缓存图片

- (NSMutableDictionary *)imgCache {if (!_imgCache) {//初始化

_imgCache =[NSMutableDictionary dictionary];

}return_imgCache;

}

这两个方法都是为了初始化刚才的两个属性

3.

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {returnself.apps.count;

}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID = @"app";

UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:ID];

DDZApp*app =self.apps[indexPath.row];

cell.textLabel.text=app.name;

cell.detailTextLabel.text=app.download;//先从内存中取出图片

UIImage *image =self.imgCache[app.icon];if(image) {

cell.imageView.image=image;

}else{//内存中没有图片//将图片文件数据写入到沙盒中

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.imgCache[app.icon] =cell.imageView.image;

}else{//下载图片

data =[NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];

cell.imageView.image=[UIImage imageWithData:data];//存到内存中

self.imgCache[app.icon] =cell.imageView.image;//将图片数据写入到沙盒中

[data writeToFile:file atomically:YES];

}

}returncell;

}

View Code

这两个方法是UITableView必须要实现的方法

第一个是返回数据量,没什么好说的

第二个是绑定数据

具体的流程看下图

ae5b106914ed9fe6c2f7271ab0d14184.png

转载地址:http://imxys.baihongyu.com/

你可能感兴趣的文章
最小权限的挑战
查看>>
jquery 视觉特效(水平滚动图片)
查看>>
SVG笔记
查看>>
linux下使用dd命令写入镜像文件到u盘
查看>>
001---进程
查看>>
视频人脸检测——OpenCV版(三)
查看>>
php获取来访者在搜索引擎搜索某个关键词,进入网站
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>
Android关于buildToolVersion与CompileSdkVersion的区别
查看>>
袋鼠云日志,日志分析没那么容易
查看>>
缓存穿透 缓存雪崩 缓存并发
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>