在UITableViewView中下载异步多个图像?

时间:2011-12-20 12:09:23

标签: iphone objective-c cocoa-touch ipad asihttprequest

如何使用ASIHttpRequest或其他有用的东西在UITableView中下载异步多个图像?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      ..........

      // Creation
      UIImageView *avatar;
      UILabel *content; 

      // Tag the IBOutlets
      avatar = (UIImageView*)[cell viewWithTag:14];
      content = (UILabel*)[cell.contentView viewWithTag:4];

      // Field
      avatar.image = image
      content.text = entryReviewtableView.content;
 }

3 个答案:

答案 0 :(得分:6)

当你可以使用GCD做一些简单的代码时,不需要为ASIHTTPRequest这样的整个框架引入依赖,只需要下载一个图像:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *imageDate = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
    dispatch_async(dispatch_get_main_queue(), ^{
        avatar.image = image;
    });
});

这是异步的,所有的善良。但是在几行代码中,您可以编写,理解,修复错误,扩展和维护自己。

答案 1 :(得分:0)

您可以使用异步图像视图而不是默认图像视图。供参考,您可以访问教程Here

答案 2 :(得分:0)

    UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];

    ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:[Array objectAtIndex:indexPath.row]]];
    [req setUsername:[NSString stringWithFormat:@"%i",i]];
    [req setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:imgV,@"imgV",nil]];
    [req setDelegate:self];
    [req startAsynchronous];
    //[imgV setContentMode:UIViewContentModeScaleToFill];
    [imgV setContentMode:UIViewContentModeScaleAspectFit];
    //[imgV setClipsToBounds:YES];
    [imgV setTag:kTagImageViewInScrollView];
    [cell addSubview:imgV];

- (void)requestFinished:(ASIHTTPRequest )request {     [(UIImageView )[[request userInfo] valueForKey:@“imgV”] setImage:[UIImage imageWithData:[request responseData]]];

[(UIActivityIndicatorView*) [(UIScrollView*) [scr viewWithTag:([[request username] intValue]+1)] viewWithTag:kActTag] removeFromSuperview];

}