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.第一次打开从网络上加载时:

2016 - 1- 19 利用多线程优化从网上加载图片的Demo

2.第二次打开时,从沙盒中加载:

2016 - 1- 19 利用多线程优化从网上加载图片的Demo

上一篇:Docker学习笔记整理


下一篇:Nova计算节点安装配置