2016 - 1- 19 利用多线程优化从网上加载图片的Demo
- //
- // ZZTableViewController.m
- // 多图片下载
- //
- // Created by Mac on 16/1/19.
- // Copyright © 2016年 Mac. All rights reserved.
- //
- #import "ZZTableViewController.h"
- #import "ZZApp.h"
- @interface ZZTableViewController ()
- // 用来存放模型
- @property (nonatomic, strong) NSArray *apps;
- @property (nonatomic, strong) NSMutableDictionary *iconDic;
- // 用于存放下载操作
- @property (nonatomic, strong) NSMutableDictionary *operations;
- @property (nonatomic, strong) NSOperationQueue *queue;
- @end
- @implementation ZZTableViewController
- - (NSOperationQueue *)queue
- {
- if (!_queue) {
- _queue = [[NSOperationQueue alloc] init];
- }
- return _queue;
- }
- - (NSMutableDictionary *)operations
- {
- if (!_operations) {
- _operations = [NSMutableDictionary dictionary];
- }
- return _operations;
- }
- - (NSMutableDictionary *)iconDic{
- if (!_iconDic) {
- _iconDic = [NSMutableDictionary dictionary];
- }
- return _iconDic;
- }
- - (NSArray *)apps
- {
- if (!_apps) {
- NSMutableArray *tmpArray = [NSMutableArray array];
- NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil];
- NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
- for (NSDictionary *dic in dicArray) {
- ZZApp *app = [ZZApp appWithdic:dic];
- [tmpArray addObject:app];
- }
- _apps = tmpArray;
- }
- return _apps;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- //#warning Incomplete implementation, return the number of sections
- return ;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- #warning Incomplete implementation, return the number of rows
- return self.apps.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *ID = @"app";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
- ZZApp *app = self.apps[indexPath.row];
- cell.textLabel.text = app.name;
- cell.detailTextLabel.text = app.download;
- UIImage *image = self.iconDic[app.icon];
- if (image) {// 先判断内存中有没有图片
- cell.imageView.image = image;
- }else{
- NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
- // 获得文件名
- NSString *fileName = [app.icon lastPathComponent];
- // 计算出文件全路径
- NSString *file = [cachePath stringByAppendingString:fileName];
- // 加载沙盒数据
- NSData *data = [NSData dataWithContentsOfFile:file];
- // 判断沙盒里有没有数据
- if (data) {// 沙盒中有数据
- cell.imageView.image = [UIImage imageWithData:data];
- // cell.imageView.image =
- // 把数据加载到内存里
- [self.iconDic setObject:cell.imageView.image forKey:app.icon];
- }else{// 下载图片
- // cell.imageView.image = [UIImage imageNamed:@"屏幕快照 2016-01-19 下午9.54.43"];
- // 添加下载操作到字典里
- NSOperation *operation = [self.operations objectForKey:app.icon];
- if (!operation) {
- operation = [NSBlockOperation blockOperationWithBlock:^{
- NSURL *url = [NSURL URLWithString:app.icon];
- NSData *data = [NSData dataWithContentsOfURL:url];
- if (!data) {// 如果下载失败,移除字典里的操作
- [self.operations removeObjectForKey:app.icon];
- return ;
- }
- // 回到主线程显示图片
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- // cell.imageView.image = [UIImage imageWithData:data];
- [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- }];
- // 放到沙盒里
- [data writeToFile:file atomically:YES];
- // 将图片存到缓存中
- [self.iconDic setObject:[UIImage imageWithData:data] forKey:app.icon];
- // 下载完成,移除操作
- [self.operations removeObjectForKey:app.icon];
- }];
- // 添加到队列里面去
- [self.queue addOperation:operation];
- // 添加到操作字典里去
- [self.operations setObject:operation forKey:app.icon];
- }
- }
- }
- return cell;
- }
- @end
效果:
1.第一次打开从网络上加载时:
2.第二次打开时,从沙盒中加载:
2016 - 1- 19 利用多线程优化从网上加载图片的Demo的更多相关文章
- (Android图片内存优化)Picasso加载图片 教程。。详细版
Picasso 是 Android 上一个强大的图片下载和缓存库. 示例代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Picasso.with( ...
- Listview 异步加载图片之优化篇(有图有码有解释)
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- Listview异步加载图片之优化篇
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目 Android-Universal-Image-Loader 或者 ignition 都是个很好的选择. 在这里把原来 ...
- requirejs:性能优化-及早并行加载
为了提高页面的性能,通常情况下,我们希望资源尽可能地早地并行加载.这里有两个要点,首先是尽早,其次是并行. 通过data-main方式加载要尽可能地避免,因为它让requirejs.业务代码不必要地串 ...
- android优化从网络中加载图片速度。。
从网络中加载图片主要要注意两个方面的问题: 1.内存管理:图片占的内存很大,假如图片数量多,很容易让系统抛出out of memory的异常. 同时我们也要注意不同android版本中内存管理的区别. ...
- 多线程异步加载图片async_pictures
异步加载图片 目标:在表格中异步加载网络图片 目的: 模拟 SDWebImage 基本功能实现 理解 SDWebImage 的底层实现机制 SDWebImage 是非常著名的网络图片处理框架,目前国内 ...
- Vue优化首屏加载
背景: 使用vue + iview搭建的一个后台管理系统,路由已经用了懒加载,加载登陆页面,居然还是需要18S左右,刚到一个新公司,项目经理很委婉的说,看看能不能优化了一下.然后就开始了网上一大堆'v ...
随机推荐
- 从别人那淘的知识 深入剖析Java中的装箱和拆箱
(转载的海子的博文 海子:http://www.cnblogs.com/dolphin0520/) 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来 ...
- 用过的正则(更新ing)
http://www.debuggex.com/ 这个很好用20120912 //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6} ...
- ARM 汇编的一些规范
A.5.1 文件格式 ARM 源程序文件(即源文件)为文件格式,可以使用任一文本编辑器编写程序代码. 在一个项目中,至少要有一个汇编源文件或C 程序文件,可以有多个汇编 ...
- Redis 学习资料整理
菜鸟爬坑--Redis学习与探索(二):Redis的数据类型 http://www.cnblogs.com/codediary/archive/2015/02/20/redisstudy-2.html ...
- java二维数组简单初步理解
二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr[0][ ...
- Servlet容器如何同时来处理多个请求
工作者线程Work Thread:执行代码的一组线程调度线程Dispatcher Thread:每个线程都具有分配给它的线程优先级,线程是根据优先级调度执行的Servlet采用多线程来处理多个请求同时 ...
- jquery $post $get $
Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. 推荐一篇不错的jQuery Ajax 实例文章,忘记了可以去看看,地址为:ht ...
- biztalk中使用WCF-SQL接受传送数据【转】
接触biztalk时间不长,转载一篇学习教程: http://www.cnblogs.com/chnking/archive/2010/05/09/1731098.html chnking写的. 一. ...
- [apache]用shell分析网站的访问情况
随着网站正式运行,我们可以通过通用的免费日志分析工具比如awstats获得一些实际访问网站的信息,例如每天ip量,pv量,用户所用的的浏览器,用户所用的操作系统等,但是有时候希望通过手工方式从WEB日 ...
- MongoDB相关资料
MongoDB的介绍及安装参考http://www.cnblogs.com/lipan/archive/2011/03/08/1966463.html 安装过程: 第一步:下载安装包:官方下载地址←单 ...