您的位置:首页 > 其它

[TwistedFate]图片异步加载,KVO

2015-12-05 14:46 190 查看

异步下载图片

自定义类imageDownLoader类

思路:

- 异步下载图片首先需要一个网址字符串 (需要传入一个字符串)

- 加载完图片数据需要将一个data传给controller显示(代理传值)

- 考虑到异步加载可能成功,也可能失败

代码实现:

- 定义协议

//  创建一个协议
@protocol ImageDownLoadDelegate <NSObject>

//  请求成功
- (void)imageDownLoadSucceedWithData:(NSData *)data;

//  请求失败
- (void)imageDownLoadFailedWithError:(NSError *)error;

@end


初始化需要的属性及方法

@interface ImageDownLoader : NSObject
//  连接
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData *data;

@property (nonatomic, assign) id<ImageDownLoadDelegate> delegate;

//  自定义初始化
- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoadDelegate>) delegate;

//  申明开始的方法
- (void)imageDownLoadStart;

//  终止方法
- (void)imageDownLoadCancel;
@end


方法实现

//  初始化方法(需要写请求中 可能用到的参数网址)
- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoadDelegate>) delegate{

self = [super init];

if (self) {

//  设置代理
self.delegate = delegate;

//  创建一个网址对象
NSURL *newUrl = [NSURL URLWithString:url];

//  根据网址创建一个请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];

//  创建链接(异步代理)
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

//[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];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

//  图片请求完成+
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoadSucceedWithData:)]) {

//  调用 协议中的方法
[_delegate imageDownLoadSucceedWithData:self.data];

}

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoadFailedWithError:)]) {

[_delegate imageDownLoadFailedWithError:error];

}

}

- (void)imageDownLoadStart{

[self.connection start];

}

- (void)imageDownLoadCancel{

[self.connection cancel];

}


KVO键值观察者

1.创建一个被观察者类ManModel

//  创建一个对象
self.man = [[ManModel alloc] init];
self.man.money = @"121312";

/**
*  KVO键值观察者
*  观察model中的某一个属性 值的变化
*  如果这个属性的值 发生变化 会触发一个方法
*  明确:
*  1.观察者   (控制器中触发一个方法 -- 比如改变视图的颜色)
*  2.被观察者 (man)
*  3.观察的属性 (对象中的 money这个属性)

*  控制器作为观察者 去观察 model(被观察者)中的一个属性的值的变化 会触发一个方法(往往是改变视图的)

*  观察者(Controller)
*  被观察者(Model)
*  Model 发生变化 去改变  视图(V)
*/

//  添加一个观察者
//  被观察者 man
//  Observer 填观察者
//  keyPath 填属性名
//  NSKeyValueObservingOptionOld原有值
//  NSKeyValueObservingOptionNew新值
//  context携带参数
[self.man addObserver:self forKeyPath:@"money" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:@"haha"];


添加一个button点击改变man的money值

- (void)click:(UIButton *)button{

self.man.money = @"das";

}


被观察者改变触发的方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

//  哪个属性发生了变化
NSLog(@"%@",keyPath);
//  object被观察的对象
NSLog(@"%@",object);
//  change值的变化
NSLog(@"%@",change);
//  context携带的参数
NSLog(@"%@",context);

//  移除观察者
[object removeObserver:self forKeyPath:keyPath];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: