摘要:1:ios通过抽象类NSOperation封装了gcd,让ios的多线程变得更为简单易用;

     2:耗时的操作交给子线程来完成,主线程负责ui的处理,提示用户的体验

     2:自定义operation继承自NSOperation,在子线程中下载图片;

   3:保证图片只下载一次,还有保证下载任务不重复

------------------------------------------------------------------------------------

实现原理:1:图片缓存:用字典保存图片和图片的url,key:url  value:图片

     2:任务缓存:用字典保存operation任务和图片的url,key:url  value:operation

先看自定义的operation:.h文件

 #import <Foundation/Foundation.h>
@class XBOperation; @protocol XBOperationDelegate <NSObject> -(void)operation:(XBOperation *)operation didFinishDownloadImage:(UIImage *)image; @end
/**
* 自定义operation
*/
@interface XBOperation : NSOperation // 代理
@property (nonatomic, weak)id<XBOperationDelegate> delegate; /**
* url
*/
@property (nonatomic, copy) NSString *url;
/**
* indexPath 和tableview中的cell相对应
*/
@property (nonatomic, strong) NSIndexPath *indexPath; @end

说明:设置代理的目的是等完成下载图片的后通知控制器拿图片

自定义operation的.m文件

@implementation XBOperation

-(void)main
{ if (self.isCancelled) return;
NSURL *downloadUrl = [NSURL URLWithString:self.url];
NSData *data = [NSData dataWithContentsOfURL:downloadUrl]; // 这行会比较耗时 if (self.isCancelled) return; UIImage *image = [UIImage imageWithData:data]; if (self.isCancelled) return;
NSLog(@"--%@--", [NSThread currentThread]); if ([self.delegate respondsToSelector:@selector(operation:didFinishDownloadImage:)]) {
dispatch_async(dispatch_get_main_queue(), ^{ // 回到主线程, 传递图片数据给代理对象
[self.delegate operation:self didFinishDownloadImage:image];
});
}
}
@end

说明:重写main方法,在把任务operation加入到队列后,队列会自动调用任务的start方法,start方法内部会调用main方法来完成操作

控制器的tableviewcell数据源的实现

#pragma mark  dataSource数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.apps.count;
} // 数据源方法 实现cell中的内容填充
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 设置数据 XBApp是数据模型
XBApp *app = self.apps[indexPath.row]; // 设置标题和子标题
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
cell.imageView.image = [UIImage imageNamed:@"57437179_42489b0"]; // 图片数据设置
UIImage *image = self.imageCache[app.icon]; if (image) { // 如果图片缓存中有图片 直接加载到
cell.imageView.image = image;
}else{ // 如果缓存中没有数据 // 先设置占位占位图片
cell.imageView.image = [UIImage imageNamed:@"57437179_42489b0"]; // 创建自定义的operation 先根据当前要显示图片的url从队列中找到operation 看是否正在下载中
XBOperation *operationDown = self.operationQueue[app.icon];
if (!operationDown) { // 如果下载operation不存在 就创建 并添加到队列中
operationDown = [[XBOperation alloc] init];
operationDown.delegate = self; // 设置代理
operationDown.indexPath = indexPath; // 把每个cell与一个operation绑定
operationDown.url = app.icon;
// 把operation添加到队列中 会自动调用start方法 然后调用operation的main的方法
[self.queue addOperation:operationDown];
// 把operation这个下载任务添加到operation的字典中 防止重复下载
self.operationQueue[app.icon] = operationDown;
} } return cell; } // 实现代理方法
-(void)operation:(XBOperation *)operation didFinishDownloadImage:(UIImage *)image
{
// 必须清楚当前operation 防止由于网络等原因造成的下载失败 而operation还在字典中 这样永远下载不了
[self.operationQueue removeObjectForKey:operation.url]; if (image) {
// 将图片放在缓存字典中
self.imageCache[operation.url] = image; // 刷新表格 单行刷新
[self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}

iOS多线程自定义operation加载图片 不重复下载图片的更多相关文章

  1. Python爬虫获取异步加载站点pexels并下载图片(Python爬虫实战3)

    1. 异步加载爬虫 对于静态页面爬虫很容易获取到站点的数据内容,然而静态页面需要全量加载站点的所有数据,对于网站的访问和带宽是巨大的挑战,对于高并发和大访问访问量的站点来说,需要使用AJAX相关的技术 ...

  2. 【iOS入门】UITableView加载图片

    学习带图片的列表 官方 LazyTableImages demo  http://download.csdn.net/detail/jlyidianyuan/5726749 分析源码是学习的好方法. ...

  3. iOS两种方式加载图片的区别

    加载图片的方式: imageNamed: imageWithContentsOfFile: 加载Assets.xcassets这里面的图片: 1> 打包后变成Assets.car 2> 拿 ...

  4. IOS中UITableView异步加载图片的实现

    本文转载至 http://blog.csdn.net/enuola/article/details/8639404  最近做一个项目,需要用到UITableView异步加载图片的例子,看到网上有一个E ...

  5. IOS 多个UIImageView 加载高清大图时内存管理

    IOS 多个UIImageView 加载高清大图时内存管理 时间:2014-08-27 10:47  浏览:59人 当我们在某一个View多个UIImageView,且UIImageView都显示的是 ...

  6. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

  7. ios客户端浏览器样式加载失效问题

    最近线上测试中出现一个奇怪的问题,ios客户端浏览器样式加载失效. 从表象来看,同样的css,安卓手机上可以正常展示,但是到ios手机上首次进入页面就不能正常显示 这时候,我们首先会考虑是不是ios设 ...

  8. iOS App中数据加载的6种方式

    我们看到的APP,往往有着华丽的启动界面,然后就是漫长的数据加载等待,甚至在无网络的时候,整个处于不可用状态.那么我们怎么处理好界面交互中的加载设计,保证体验无缝衔接,保证用户没有漫长的等待感,而可以 ...

  9. wp加载本地HTML(附带图片,CSS,JS)

    wp加载本地HTML(附带图片,CSS,JS) Windows Phone:Load Local HTML with Img,Css,Js by 唐小崇 http://www.cnblogs.com/ ...

随机推荐

  1. 017Makefile工程管理

    1.为什么需要Makefile? 利用Makefile和make的合作,可以把很多很多的工作合并成一个非常简单的命令:make: make能够使整个程序的编译.链接只需要一个命令(make)就可以完成 ...

  2. 支持在安卓中UI(View)的刷新功能

     这是一款可以支持在安卓中UI(View)的刷新功能,Android中对View的更新有很多种方式,使用时要区分不同的应用场合.我感觉最要紧的是分清:多线程和双缓冲的使用情况.   现在可以尝试理解下 ...

  3. GoLang安装

    GoLang的官网被墙,镜像下载地址:http://tip.golang.so/dl/  或者 http://golang.so/dl/ 安装说明:http://tip.golang.so/doc/i ...

  4. php 执行事务的时候pdo出现问题

    新版本的pdo会有这个问题: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...

  5. 搭通自己的电脑与GitHub的传输通道

    一.远程仓库怎么玩 1. 自己搭建一个运行Git的服务器 Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上,但肯定有一台机器有着最原始的版本库,然后别的机器来克隆这个原始版本库,这 ...

  6. MySql like模糊查询使用详解

    一.SQL的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零个字符).在 MySQL中,SQL的模式缺省是忽略大小写的.下面显示一些例子.注意在你使用SQL模式时,你不能使用 ...

  7. Eclipse编码问题

    通常在Eclipse下,mac和windows编码是不一样的.如果含有中文java sources通常会出现乱码. 解决---小程序! import java.io.File; import java ...

  8. Laravel 5 基础(七)- Eloquent (laravel 的ORM)

    我们来生成第一个模型 php artisan make:model Article #输出 Model created successfully. Created Migration: 2015_03 ...

  9. pure css兼容IE

    <!--[if lte IE 8]> <link rel="stylesheet" href="pure/0.5.0/grids-responsive- ...

  10. LinearRegression

    利用python实现简单的线性回归对房屋面积进行预测 # -*-coding:utf-8 -*- ''' Created on 2016年12月15日 @author: lpworkdstudy '' ...