简体   繁体   English

tabelviewcell中的异步图像下载?

[英]asynchronous image downloading in tabelviewcell?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"cell";
     TVcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         cell = [[TVcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }
     cell.titleLabel.text = [[[arrayData objectAtIndex:indexPath.row]valueForKey:@"title"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

     cell.txtlabel.text = [[[arrayData objectAtIndex:indexPath.row] valueForKey:@"description"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
     cell.tag = indexPath.row;
     cell.imageView.image = nil;
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
     dispatch_async(queue, ^(void) {

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[enclosureUrlArray objectAtIndex:indexPath.row]]];

     UIImage* image = [[UIImage alloc] initWithData:imageData];
     if (image) {
         dispatch_async(dispatch_get_main_queue(), ^{
             if (cell.tag == indexPath.row) {
                 cell.IMGLabel.image = image;
                 [cell setNeedsLayout];
                 }
              });
          }
      });
      return cell;

}

while i'm scrolling that same images are appearing on the cells,which cells im reusing.i used scroll view delegate also.im storing urls in enclosureUrlArray and i'm passing. 当我滚动显示相同的图像出现在单元格上时,这些单元格正在重复使用。我也使用了滚动视图委托。我将URL存储在enclosureUrlArray中并且正在传递。

Put cell.IMGLabel.image = nil in your cellForRowAtIndexPath before downloading image, ie below cell.imageView.image = nil; 在下载图像之前,将cell.IMGLabel.image = nil放在cellForRowAtIndexPath ,即在cell.imageView.image = nil; this line. 这条线。

Or set some place holder image (no image available kind of image to cell's image in interface builder), so if image is not downloaded then it will show this place holder image otherwise it will show downloaded image. 或设置一些占位符图像(界面生成器中没有可用的图像种类到单元格图像),因此,如果未下载图像,它将显示此占位符图像,否则将显示下载的图像。

SDWebImage is good for this kind of case. SDWebImage适用于这种情况。 It will cache the images so it will give better performance also. 它将缓存图像,因此也将提供更好的性能。 There is nothing wrong to use any good third party library. 使用任何好的第三方库都没有错。

By using SDWeb image: 通过使用SDWeb图像:

#import <SDWebImage/UIImageView+WebCache.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided sd_setImageWithURL: method to load the web image
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

You can also use NSURLSession as well,see this link 您也可以使用NSURLSession,请参见此链接

Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling 从UITableView单元内的URL加载异步图像-滚动时图像变为错误的图像

 NSURL *url = [NSURL URLWithString:[enclosureUrlArray objectAtIndex:indexPath.row]];
 NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data) {
        UIImage *image = [UIImage imageWithData:data];
        if (image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                MyCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
                if (updateCell)
                    updateCell.IMGLabel.image = image;
            });
        }
    }
}];
[task resume];

works perfect for me.. 对我来说很完美。

with the help of sdwebimage you can show the image like this:- 借助sdwebimage,您可以显示如下图像:-

NSString *string2 = [[businessListArray objectAtIndex:indexPath.row ]valueForKey:@"logo"];
    NSURL *url1 = [NSURL URLWithString:
                   [NSString stringWithFormat:
                    @"%@%@",
                    @"http://dev-demo.info.bh-in-15./upload/post/",
                    string2]];

    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:301];

    [imageView sd_setImageWithURL:url1
                 placeholderImage:[UIImage imageNamed:@"pocket.png"]
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            imageView.image = image;
                        }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM