TableView_图片异步加载 KVO
TableView 异步下载图片
ImageDownloader.h
#pragma mark - 声明block
//1,声明block
typedef void(^Result) (UIImage *img);
@interface ImageDownloaderViewController : UIViewController
#pragma mark - 声明方法
//block 做参数
//ImageDownloader 允许外界指定URL,提供 开始下载 和 取消下载 功能,并提供delegate或block将图⽚片传递给外界。
+ (void)imageDownloaderWithUrlStr:(NSString *)urlStr result:(Result)result;
ImageDownloader.m
+ (void)imageDownloaderWithUrlStr:(NSString *)urlStr result:( Result)result
{
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//把请求的二进制数据 转成图片
UIImage *image = [UIImage imageWithData:data];
//回到主线程调用
dispatch_sync(dispatch_get_main_queue(), ^{
//调用block
result(image);
});
}];
}
TableViewController.m
//加载数据
- (void)loadDataAndShow
{
NSURL *url = [NSURL URLWithString:URL_NEW];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
__block NewsTableViewController *weakSelf = self;
NSOperationQueue *queue = [[NSOperationQueue new] autorelease];
//建立连接,
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//取大字典数据
//从key为result的数组中 取字典
//在此循环完成 即图片赋值完成
for (NSDictionary *dic in dict[@"result"]) {
//创建模型
News *new = [News new];
NSLog(@"模型创建,图片 开始下载");
//此时 图片开始下载
[new setValuesForKeysWithDictionary:dic];
[weakSelf.allDataMutableArray addObject:new];
[new release];
}
//回主线程
dispatch_sync(dispatch_get_main_queue(), ^{
//先刷新UI再拿到图片
[weakSelf.tableView reloadData];
NSLog(@" 刷新UI");
});
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return _allDataMutableArray.count;
}
//显示Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" 显示 一个 cell");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"new" forIndexPath:indexPath];
// Configure the cell...
News *new = _allDataMutableArray[indexPath.row];
cell.textLabel.text = new.movieName;
cell.detailTextLabel.text = new.movieId;
//如果图片数据请求下来,就赋值显示。如果没有,就监听
if (new.imageData != nil) {
cell.imageView.image = new.imageData;
}else{
NSLog(@" 图片没请求下来---》》》监听 ");
//KVO (1,注册监听)设置监听图片数据
//new 被监听
//self 监听者
//keypath 监听属性
//options 监听旧值 还是 新值
//控制器 观察 -》模型(在控制器中观察)
[new addObserver:self forKeyPath:@"imageData" options:NSKeyValueObservingOptionNew context:indexPath];
}
return cell;
}
#pragma mark - (2,实现回调方法) KVO 监听被执行的方法,对象属性变化,即调用
//tableView 通过KVO观察 下载数据 及时更新页面
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//判断标志
if ([keyPath isEqualToString:@"imageData"]) {
//1, 获取新图片数据
UIImage *newImageData = change[NSKeyValueChangeNewKey];
//2, 获取cell
NSIndexPath *indexPath = (NSIndexPath *)context;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = newImageData;
//3, 刷新当前行
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//4,取消监听
[object removeObserver:self forKeyPath:keyPath];
NSLog(@"(监听值变化)拿到图片了。。。。。。");
}
}
//模型.h
@interface News : NSObject
@property(nonatomic,copy)NSString *movieId;
@property(nonatomic,copy)NSString *movieName;
@property(nonatomic,copy)NSString *pic_url;
//Model类注意事项:
//1、除了包含必要的数据外,还要包含⼀一个ImageDownloader对象。
//2、包含⼀一个image。
//3、包含⼀一个图⽚片是否正在下载的BOOL值。(⽤用于判断是否需要开始 下载)
@property(nonatomic,retain) UIImage *imageData;
//模型.m
#pragma mark - 重写pic_url 的 setter
//为了能正确显示图片,Model类应该提供图片获取功能(Model通过 ImageDownloader下载图片,供cell使用)
- (void)setPic_url:(NSString *)pic_url
{
if (_pic_url != pic_url) {
[_pic_url release];
_pic_url = [pic_url retain];
//
//使用block 异步加载图片
__block News *weakNews = self;
[ImageDownloaderViewController imageDownloaderWithUrlStr:self.pic_url result:^(UIImage *img) {
//将block 显示到imageView上
//在这使用,就在这实现(模型中实现block)
weakNews.imageData = img;
NSLog(@"--------------拿到图片,赋值显示 NEws");
}];
}
}
TableView_图片异步加载 KVO的更多相关文章
- 图片异步加载 ,KVO
图片异步下载类目: .h #import <UIKit/UIKit.h> typedef void (^ImageBlock)(UIImage *img); @interface UIIm ...
- Android-Universal-Image-Loader 图片异步加载类库的使用
在博客中看到一篇利用组件进行图片异步加载的文章在此作记录 原文:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载并缓存的 ...
- Android图片异步加载框架Android-Universal-Image-Loader
版权声明:本文为博主原创文章,未经博主允许不得转载. Android-Universal-Image-Loader是一个图片异步加载,缓存和显示的框架.这个框架已经被很多开发者所使用,是最常用的几个 ...
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...
- Android ListView 图片异步加载和图片内存缓存
开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...
- Android 图片异步加载的体会,SoftReference已经不再适用
在网络上搜索Android图片异步加载的相关文章,目前大部分提到的解决方案,都是采用Map<String, SoftReference<Drawable>> 这样软引用的 ...
- 【转】Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
Android-Universal-Image-Loader 原文地址:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载 ...
- Android图片异步加载之Android-Universal-Image-Loader
将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...
- Android图片异步加载之Android-Universal-Image-Loader(转)
今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...
随机推荐
- Binary Search Tree Iterator——LeetCode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Java学习日记-1 设置Java环境变量等
一.环境变量的设置 安装完jdk以后,需要配置环境变量,找到[我的电脑]-[属性]-[高级]-[环境变量] 这里需要配置3个环境变量 1.新建系统变量JAVA_HOME,变量值为jdk的安装路径,比如 ...
- hdu-1010 dfs+剪枝
思路: 剪枝的思路参考博客:http://www.cnblogs.com/zibuyu/archive/2012/08/17/2644396.html 在其基础之上有所改进 题意可以给抽象成给出一个 ...
- ASP.NET NuGet to install the mvc 5.2.2
http://www.nuget.org/packages/Microsoft.AspNet.Mvc
- OpenGL —— 基础笔记
1.基础博文 链接:http://blog.csdn.net/lotusone?viewmode=contents 2.绘制文字 链接:http://www.cnblogs.com/xia ...
- IntelliJ IDEA 中module的dependencies是其它module时的注意事项
Dependencies on other modules If a module (module A) depends on another module (module B), IntelliJ ...
- Java基础知识强化85:System类之arraycopy()方法(数组拷贝)
1. arraycopy方法(数组拷贝) public static void arraycopy(object src,int srcPos,Object dest,int destPos, int ...
- html:标签原本属性
<!doctype html>无标题文档 a标签,默认有text-decoration属性 span标签不需要清零 b标签不需要清零 em标签不需要清零 strong 相邻内嵌元素代码里面 ...
- 关于在repeater中的checkbox实行多选和全选
今天项目中用到这一块,是一个b2b商城,业务是别人给客户留言后,客户从会员中心的留言管理中查看,用checkbox实行多选和全选后进行批量审核 首先在checkbox后加个hidden,作用见代码: ...
- 增加Android模拟器的内存
1,在window中,打开'C:\Users\Administrator\.android\avd\4.4.2.avd\config.ini'文件(我的是win7,xp的貌似不是'Users',是'D ...