iOS 之 AFImageRequestOperation的使用

来源:互联网 发布:带着淘宝穿古代 编辑:程序博客网 时间:2024/06/01 09:16

刚刚更新iOS7 系统默认是ARC 的工程, 之前一直用的ASI  没有支持ASI 的(外加ASI 已经不更新了)。。所以这个项目就转战AFNetWork 了  (由于 系统还要支持 iOS 5.0的版本) 所以用的并不是AF2.0 

使用中 发现   AFImageRequestOperation 的使用 和JSON /XML 等并不一样。所以在这里简单的说一下。 希望能够帮助到大家。也是自己博客生涯的开始吧!


首先 发一个JSON 的 做对比

 AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWithRequest:request success:^(NSURLRequest *request,NSHTTPURLResponse *response, id JSON) {


/*这个位置*/

        NSArray * array = [JSON objectForKey:@"data"];

       

    } failure:^(NSURLRequest *request,NSHTTPURLResponse *response, NSError *error,id JSON) {

        UIAlertView *av = [[UIAlertViewalloc] 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 = [AFImageRequestOperationimageRequestOperationWithRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:identifier]] imageProcessingBlock:nil

                                                                                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)

                                          {

                                              completionBlock(image);

                                          }

                                                                                          failure:nil];

    

    [operation start];

}


然后在其他位置 调用改方法

if([fm fileExistsAtPath:_imagePath]){

        [_imageArrayaddObject:[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




原创粉丝点击