iOS 之 AFImageRequestOperation的使用
2013-10-14 23:06
204 查看
刚刚更新iOS7 系统默认是ARC 的工程, 之前一直用的ASI 没有支持ASI 的(外加ASI 已经不更新了)。。所以这个项目就转战AFNetWork 了 (由于 系统还要支持 iOS 5.0的版本) 所以用的并不是AF2.0
使用中 发现 AFImageRequestOperation 的使用 和JSON /XML 等并不一样。所以在这里简单的说一下。 希望能够帮助到大家。也是自己博客生涯的开始吧!
首先 发一个JSON 的 做对比
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
/
*这个位置*/
NSArray * array = [JSON
objectForKey:@"data"];
} failure:^(NSURLRequest *request,
NSHTTPURLResponse *response, NSError *error,
id JSON) {
UIAlertView *av = [[UIAlertView
alloc] initWithTitle:@"Error Retrieving Weather"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}];
[operation start];
在看下 Image的
- (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier {
AFImageRequestOperation* operation = [AFImageRequestOperation
imageRequestOperationWithRequest:[NSURLRequest
requestWithURL:[NSURL
URLWithString:identifier]]
imageProcessingBlock:nil
success:^(NSURLRequest *request,
NSHTTPURLResponse *response, UIImage *image)
{
completionBlock(image);
}
failure:nil];
[operation start];
}
然后在其他位置 调用改方法
if([fm fileExistsAtPath:_imagePath]){
[_imageArray
addObject:[UIImage imageWithContentsOfFile:_imagePath]];
}else{
[self downloadImageWithCompletionBlock:^(UIImage *downloadedImage) {
[_imageArray addObject:downloadedImage];
NSLog(@"array = %@",_imageArray);
NSLog(@"222222222");
// stop updating download progress UI
} identifier:imageName];
NSLog(@"1111111111");
}
这里在于Image 不像 JSON 都是文字的 处理的快。 这里需要网络请求一段时间。所以不能够直接使用。需要 用得了方法检测 请求完成。。
试下用我上面的方法 (没有图片时 会先输出1111 在输出22222) ;
不在赘述。使用看看。(方法也是在别人那直接 拿的 )
刚接触iOS不久 希望能多认识写同道中人
QQ群:175314871
使用中 发现 AFImageRequestOperation 的使用 和JSON /XML 等并不一样。所以在这里简单的说一下。 希望能够帮助到大家。也是自己博客生涯的开始吧!
首先 发一个JSON 的 做对比
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
/
*这个位置*/
NSArray * array = [JSON
objectForKey:@"data"];
} failure:^(NSURLRequest *request,
NSHTTPURLResponse *response, NSError *error,
id JSON) {
UIAlertView *av = [[UIAlertView
alloc] initWithTitle:@"Error Retrieving Weather"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}];
[operation start];
在看下 Image的
- (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier {
AFImageRequestOperation* operation = [AFImageRequestOperation
imageRequestOperationWithRequest:[NSURLRequest
requestWithURL:[NSURL
URLWithString:identifier]]
imageProcessingBlock:nil
success:^(NSURLRequest *request,
NSHTTPURLResponse *response, UIImage *image)
{
completionBlock(image);
}
failure:nil];
[operation start];
}
然后在其他位置 调用改方法
if([fm fileExistsAtPath:_imagePath]){
[_imageArray
addObject:[UIImage imageWithContentsOfFile:_imagePath]];
}else{
[self downloadImageWithCompletionBlock:^(UIImage *downloadedImage) {
[_imageArray addObject:downloadedImage];
NSLog(@"array = %@",_imageArray);
NSLog(@"222222222");
// stop updating download progress UI
} identifier:imageName];
NSLog(@"1111111111");
}
这里在于Image 不像 JSON 都是文字的 处理的快。 这里需要网络请求一段时间。所以不能够直接使用。需要 用得了方法检测 请求完成。。
试下用我上面的方法 (没有图片时 会先输出1111 在输出22222) ;
不在赘述。使用看看。(方法也是在别人那直接 拿的 )
刚接触iOS不久 希望能多认识写同道中人
QQ群:175314871
相关文章推荐
- Android编程方式开发UI界面和XML文件与Java代码混合控制UI界面
- "I need a build...", "Here you are!"
- 【Ogre Beginner Guider】第一章 配置OGRE
- marquee实现内容滚动
- 同事的uiscrollview无限滑动
- UIView类动画效果 commit后如何停止
- 关于UIView的autoresizingMask属性的研究
- canfestival的PKGBUILD
- POJ 1141 - Brackets Sequence 区间DP
- 黑马程序员_图形用户界面GUI
- POJ 2778 DNA Sequence && AC自动机 矩阵 矩阵加速
- poj 1904 King's Quest(强连通分量)
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
- IOS 代码总结 UIImage UILabel UIButton
- ActionContext、ValueStack、Stack Context之间的关系
- Children of the Candy Corn 深搜 广搜 很经典 对queue的认识又增加一份
- IOS控件之UITableView
- IOS控件之分隔条控制器UISplitViewController
- Phone界面上文本框UITextField的软键盘隐藏和移动键盘下面的内容和限制字数
- hdu 4441 Queue Sequence (伸展树splay+树状数组)