ImageDownLoaderAndKVO
2015-09-16 21:08
211 查看
//开始下载 - (IBAction)startDownLoader:(id)sender { self.imageDownLoader = [[ImageDownLoader alloc] initWithImageURL:kPicURL delegate:self]; } //实现方法 -(void)imageDownLoader:(ImageDownLoader *)imageDownLoader didSuccessedWithImage:(UIImage *)image { self.imageView.image = image; } //结束下载 - (IBAction)cancelDownLoader:(id)sender { [self.imageDownLoader cancel]; }
//初始化方法 -(id)initWithImageURL:(NSString *)imageURL delegate:(id<imageDownLoaderDelegate>)delegate { self = [super init]; if (self) { //给delegate 赋值 self.delegate = delegate; //进行请求 [self requestImageWithImageURL:imageURL]; } return self; } //取消下载 -(void)cancel { [self.connection cancel]; } -(void)requestImageWithImageURL:(NSString *)imageURL { //1.创建URL //再编码 NSString *newStr = [imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //创建 NSURL *url = [NSURL URLWithString:newStr]; //2.创建请求对象 NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.请求并设置代理 self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; } //链接到服务器 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if (_data == nil) { self.data = [NSMutableData data]; } } //获取数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //拼接 [_data appendData:data]; //进行数据操作 } //下载完成 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { //转化图片 UIImage *image = [UIImage imageWithData:_data]; //通知代理 将图片拿走 [_delegate imageDownLoader:self didSuccessedWithImage:image]; }
//给person对象添加观察者 [self.person addObserver:self forKeyPath:@"name" options:(NSKeyValueObservingOptionNew) context:@"西兰花"];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"keyPath : %@",keyPath); NSLog(@"object : %@",object); NSLog(@"change :%@",change); NSLog(@"context : %@",context); self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.nameTf endEditing:YES]; //使用输入框内的名字 给person对象赋值 self.person.name = self.nameTf.text; //[self.nameTf resignFirstResponder]; }
相关文章推荐
- SSH-Struts(两)—调节器(ActionServlet)
- 【待】1.4 Write a method to replace all spaces in a string with'%20'.
- hdu 4540 威威猫系列故事——打地鼠
- Java中的“别名现象”
- 域名那些事儿
- Oracle学习之常见问题处理
- hdu 5446
- 从评教移动端分析MVC
- 关于cocos2d-js中使用 ClippingNode 以及 BlendFunc 来实现遮罩
- matlab中简单的图像旋转算法
- 获取Mac地址
- Android自定义View之三种流行进度条的写法
- hdu 5446
- CSS 基础
- this 指针
- HDU 1536 S-Nim(sg函数)
- javaEE --- Hibernate
- Effective C++——条款43(第7章)
- NetworkRequest 网络请求
- 前端之路