簡體   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;

}

當我滾動顯示相同的圖像出現在單元格上時,這些單元格正在重復使用。我也使用了滾動視圖委托。我將URL存儲在enclosureUrlArray中並且正在傳遞。

在下載圖像之前,將cell.IMGLabel.image = nil放在cellForRowAtIndexPath ,即在cell.imageView.image = nil; 這條線。

或設置一些占位符圖像(界面生成器中沒有可用的圖像種類到單元格圖像),因此,如果未下載圖像,它將顯示此占位符圖像,否則將顯示下載的圖像。

SDWebImage適用於這種情況。 它將緩存圖像,因此也將提供更好的性能。 使用任何好的第三方庫都沒有錯。

通過使用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;
}

您也可以使用NSURLSession,請參見此鏈接

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

對我來說很完美。

借助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