Download an asynchronous multiple images in UITableViewView?

元气小坏坏 提交于 2020-01-04 15:33:34

问题


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

}



来源:https://stackoverflow.com/questions/8575229/download-an-asynchronous-multiple-images-in-uitableviewview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!