UI一揽子计划 17 (image的异步加载、KVO观察者模式、KVO进行豆瓣列表界面图片的异步加载)
来源:互联网 发布:mysql爆破 编辑:程序博客网 时间:2024/05/22 07:42
@protocol ImageDownloaderDelegate <NSObject>
// 成功 把data 传递出去
- (void)imageDonwloadSuccessedWithData:(NSData *)data;
- (void)imageDonwloadFailedWithError:(NSError *)error;
// 失败 把失败原因传递出去
@end
@interface ImageDownloader : NSObject
// 写一个代理的属性
@property (nonatomic,assign)id<ImageDownloaderDelegate> delegate;
// 声明初始化方法
- (instancetype)initWithImageURL:(NSString *)imageUrl delegate:(id<ImageDownloaderDelegate>)delegate;
// 定义成外部类
- (void)start;
- (void)cancel;
// 没有延展 自己写
@interface ImageDownloader ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
@property (nonatomic,retain)NSMutableData *data;
@property (nonatomic,retain)NSURLConnection *connection;
@end
@implementation ImageDownloader
- (void)dealloc
{
// 销毁/终止请求 最好写在控制器里
// [self cancel];
[_connection release];
[_data release];
[super dealloc];
}
- (instancetype)initWithImageURL:(NSString *)imageUrl delegate:(id<ImageDownloaderDelegate>)delegate
{
self = [super init];
if (self) {
// 进行初始化 图片的请求
NSURL *imageURL = [NSURL URLWithString:imageUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imageURL cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];
request.HTTPMethod = @"GET";
self.delegate = delegate;
// 请求创建一个链接
// 使用代理方法
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
// 开启
// 最好是本类的对象 控制 增加灵活性
[self start];
// [self.connection start];
}
return self;
}
// 接受到服务器的信息 说明链接成功了 开始初始化
// 开始
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.data = [NSMutableData data];
}
// 中
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
NSLog(@"加载中...");
}
// 结束
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 使用代理 把data 传给控制器(因为 imageView 在控制器里创建的)
// 代理不为空 并且代理实现了协议中的方法
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDonwloadSuccessedWithData:)]) {
[_delegate imageDonwloadSuccessedWithData:self.data];
}
}
// 报错
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDonwloadFailedWithError:)]) {
[_delegate imageDonwloadFailedWithError:error];
}
{
[self.connection start];
{
[self.connection cancel];
}
KVO 键值观察者,是观察者设计模式的一种具体实现
KVO触发机制:一个对象(观察者),监测另一个对象(被观察者)的某属性是否发生变化,或被监测的属性发生更改,会触发观察者的一个方法(方法名固定好,类似代理方法)
KVO使用步骤:
1.注册观察者(为被观察者制定观察以及被观察属性)
addObserver添加一个观察者,forKeyPath被观察者的某个属性,options观察的变化,新的和老的,context上下文,可以是nil也可以携带参数.
[self.pangWeiSuoaddObserver:selfforKeyPath:@"hobby"options:(NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld)context:nil];
2.实现回调方法
其中KeyPath是观察的属性,object为被观察的对象,change为一个字典有new(新值)old(旧值),context为穿过来的值
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// 移除观察者
[object removeObserver:selfforKeyPath:@"hobby"];
}
3.触发回调方法(被观察属性发生更改)
self.pangWeiSuo.hobby= @"又胖又猥琐";
4.移除观察者
KVO使用场景:MAC中M与C通信,M发生变化通知C.其中M是被观察者,C是观察者
KVO注意事项:观察者销毁之前,移除观察者,否则会出现程序异常(给以及销毁的对象发送消息)
KVO监测Model图片下载
1.在model中声明需要用到的属性
// 声明一个连接,来保存连接图片
@property (nonatomic,retain) UIImage *getUrlImage;
// 图片加载的状态
@property (nonatomic,assign) BOOL isDownLoading;
// 把加载图片的类声明成属性
@property (nonatomic,retain)ImageDownLoader*imageDownLoad;
- (void)imageDownLoader;
2.遵循ImageDownloaderDelegate的协议,实现协议中的两个方法
- (void)imageDownSucceedWithDate:(NSData *)data
{
// 给model里的图片赋值,
self.getUrlImage = [UIImageimageWithData:data];
// 加载完毕修改状态
self.isDownLoading= NO;
}
-(void)imageDownFailedWithDate:(NSError *)error
{
// 标识加载失败
self.isDownLoading= NO;
NSLog(@"%@", error);
}
3.在model中添加一个开始的方法用于调用
- (void)imageDownLoader
{
self.imageDownLoad= [[ImageDownLoaderalloc] initWithImageURL:self.imagedelegate:self];
[self.imageDownLoadstart];
// 标识正在加载
self.isDownLoading= YES;
}
4.在dealloc 释放
- (void)dealloc
{
[_imageDownLoadcancel];
}
5.在controller的设置cell的方法中,给model添加观察者
ActivityModel *model = self.dataArray[indexPath.row];
// 给model添加观察者
// 需要观察的是图片空的和未加载完成的时候
if (model.getUrlImage ==nil && model.isDownLoading ==NO){
// 添加观察者
[model addObserver:selfforKeyPath:@"getUrlImage"options:(NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld)context:indexPath];
[model imageDownLoader];
//添加一个占位符
cell.activityImageView.image = [UIImageimageNamed:@"picholder"];
} else {
//证明已经有图片了,把图片给cell
cell.activityImageView.image= model.getUrlImage;
}
6.在观察者触发的方法中把model中的图片赋值为cell
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// 取出model对应的cell
NSIndexPath *indexPath = (NSIndexPath *)context;
ActivityListCell *cell = (ActivityListCell *)[self.tableViewcellForRowAtIndexPath:indexPath];
cell.activityImageView.image = change[@"new"];
// 移除观察者
- UI一揽子计划 17 (image的异步加载、KVO观察者模式、KVO进行豆瓣列表界面图片的异步加载)
- UI 17 异步加载图片 KVO
- [TwistedFate]图片异步加载,KVO
- IOS中如何异步加载图片(三)KVO观察者
- UI课程17 图片异步下载、KVO
- UI基础-图片异步下载、KVO
- 图片的异步加载
- 异步的图片加载
- 图片的异步加载
- 图片的异步加载
- 图片的异步加载
- 图片的异步加载
- 基于kvo的观察者模式
- Universal-Image-Loader 图片异步加载类库的使用
- 异步加载图片框架Android-Universal-Image-Loader的使用
- 异步加载网络图片之Universal-Image-Loader的使用
- Image-Loader 图片异步加载类库的使用
- 安卓universal-image-loader图片异步加载的解析
- NsDate
- 使用python进行文本分类
- iOS9适配
- 面试官有问题?我看是你有问题吧!
- JSON.NET 简单的使用
- UI一揽子计划 17 (image的异步加载、KVO观察者模式、KVO进行豆瓣列表界面图片的异步加载)
- 开发者如何演示App功能给客户
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- Ubuntu 14.04 LTS下安装Google Chrome浏览器
- Servlet学习
- hibernate 的缓存机制?
- scala入门测试
- UI一揽子计划 18 (沙盒机制、简单对象写入文件、NSFileMange、复杂对象写入文件)
- 方法重载和方法重写的区别