簡體   English   中英

在uitableviewcell中異步加載圖片

[英]Asynchronously load image in uitableviewcell

我試圖異步加載UITableVIew的圖像。

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

UITableViewCell *cell = nil;
NSString *cellIndetify = [NSString stringWithFormat:@"cell%d",tableView.tag -TABLEVIEWTAG];

cell = [tableView dequeueReusableCellWithIdentifier:cellIndetify];
IndexPath *_indextPath = [IndexPath initWithRow:indexPath.row  withColumn:tableView.tag - TABLEVIEWTAG];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor blackColor];
// here I init cell image view
        UIView *cellSub =  [self.waterFlowViewDatasource waterFlowView:self cellForRowAtIndexPath:_indextPath];

    cellSub.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:cellSub];
    cellSub.tag = CELLSUBVIEWTAG;

}
// fill image info 
    [self.waterFlowViewDatasource waterFlowView:self fillDataIntoCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath];


CGFloat cellHeight = [self.waterFlowViewDelegate waterFlowView:self heightForRowAtIndexPath:_indextPath];

CGRect cellRect = CGRectMake(0, 0, _cellWidth, cellHeight);
[[cell viewWithTag:CELLSUBVIEWTAG] setFrame:cellRect];


[self.waterFlowViewDatasource waterFlowView:self relayoutCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath];

return cell;
}

在該ImageView類中,我進行了初始化:

(id)initWithIdentifier:(NSString *)indentifier
{
    if(self = [super initWithIdentifier:indentifier])
    {
        self.backgroundColor = [UIColor blackColor];

        if (!self.imageView) {
            _imageView = [[UIImageView alloc] init];
            self.imageView.backgroundColor = IMAGEVIEWBG;
            [self addSubview:self.imageView];

            self.imageView.layer.borderWidth = 1;
            self.imageView.layer.borderColor = [[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0] CGColor];
        }
        ......some others controllers
}

-(void)setImageWithURL:(NSURL *)imageUrl{


    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromKey:[imageUrl absoluteString]];
    if (cachedImage) {
        self.imageView.image = cachedImage;
    }else{
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
            [manager downloadWithURL:imageUrl delegate:self options:SDWebImageCacheMemoryOnly userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[imageUrl absoluteString], @"image_url", [NSValue valueWithBytes:&imageSize objCType:@encode(CGSize)], @"image_size", [NSNumber numberWithInt:arrIndex], @"imageview_index", nil]];
            _imageView.image = [UIImage imageNamed:@"album_detail_placeholder.png"];
}
.....
.....

- (void)imageDecoder:(SDWebImageDecoder *)decoder didFinishDecodingImage:(UIImage *)image userInfo:(NSDictionary *)userInfo {

       imageView.image = image
}

如您所見,我使用了SDWebImageManager,但這似乎不是問題。

當拉到下一頁時,在加載下一頁圖像之前,如果我滾動回到上一頁的加載圖像,則該圖像將被另一張圖像覆蓋(應該顯示在最新頁面中,而不是當前頁面中……) (例如,在第一頁中,我有一個圖像,並且該圖像中有貓,然后向下滾動直到拉動以請求下一頁,並在獲取所有下一頁圖像的url之后,啟動異步線程下載圖像,然后下載最新的圖像,然后滾動到頂部,也許是第一頁。一段時間后,一些圖像下載,它們應該顯示在該單元格中,但是現在這些圖像將覆蓋當前頁面的圖像,也許貓的圖像上現在是狗的圖像) 我該怎么辦?

當一行在屏幕外滾動時,表視圖將該行的單元格重用於屏幕上滾動的另一行。

問題是您將ImageView用作后台加載程序的委托,並且ImageView與單元而不是行關聯。 到背景加載程序完成加載圖像時,該單元可能已被重新用於另一行。

您需要使表格視圖的數據源成為背景圖像加載器的委托。 調用downloadWithURL:delegate:options:userInfo:時,應將行的索引路徑放入userInfo字典中。 當下載程序將imageDecoder:didFinishDecodingImage:userInfo:發送到數據源時,數據源應從userInfo獲取索引路徑,並使用它向表視圖詢問當前顯示該行的單元格(通過將cellForRowAtIndexPath:發送到表中)視圖)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM