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 ...
随机推荐
- css3开门
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- js数据类型和关系运算语法
var box=; alert(typeof box); //box是Undefined类型,值是undefined,类型返回的字符串是undefined var box=true; alert(ty ...
- [工作需求]linux常用命令以及vim常用命令
一. Linux 常用命令 mkdir dirname新建文件夹 cd ~ 进入自己的家目录 cd dirname 进入名字为dirname的目录: l 显示当前文件夹下的文件 ...
- 在eclipse导入项目的步骤【转】
1. Import 2. Next 3. 确定 选中copy projects into workspace Finish 这样项目就导入进来了. 4.导入jar包 Configure Bui ...
- Centos 6.2 安装mysql5.5
1. 安装mysql 相关依赖库(没有的话就安装,有就不用安装了) 通过 rpm -qa | grep name 的方式验证以下软件包是否已全部安装. gcc* gcc-c++* autoconf* ...
- 搜索功能demo
代码如下: <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edg ...
- python 爬虫
import urllib2 as url import re urls = 'http://www.php100.com/html/it/' headers = {'User-Agent':'Moz ...
- TopCoder SRM 582 ColorfulBuilding
DP 思路是三维,但是时间肯定会超时,需要根据其特殊性质加两个标记数组,优化成二维. 刚开始想了N久N久,没感觉,还是动手画了一下才有用呀,意淫再久,不如动手呀. 代码: #include<i ...
- 2440 lcd10分钟休眠修改
在我们的系统中,LCD的虚拟控制台和控制台TTY不是同一个设备,也就是说,如果在程序里单纯的printf是不行的!这样只能修改你正在使用的TTY的blankinterval,而你用的却是文本方式的设备 ...
- Validform自定义提示效果-使用自定义弹出框
$(function(){ $.Tipmsg.r=null; $("#add").Validform({ tiptype:function(msg){ layer.msg(msg) ...