Download an asynchronous multiple images in UITabl

2019-07-18 14:45发布

问题:

How can i download an asynchronous multiple images in the UITableView using ASIHttpRequest or something useful?

 - (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;
 }

回答1:

No need to introduce a dependency to a whole framework such as ASIHTTPRequest just to download one image, when you can do it a few easy lines of code using GCD:

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;
    });
});

This is asynchronous and all the goodness. But in a few lines of code you can write, understand, bug-fix, extend and maintain yourself.



回答2:

You can be use the asynchronous image view instead of the default image view. for reference you can visit tutorial Here.



回答3:

    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];

}