iOS 之 AFImageRequestOperation的使用

刚刚更新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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值