简体   繁体   English

从UITableView单元格中的URL加载异步图像 - 滚动时图像变为错误的图像?

[英]Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling?

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

    TVcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil)
        cell = [[TVcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

   cell.txtlabel.text = [[[arrayData objectAtIndex:indexPath.row] valueForKey:@"description"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    cell.IMGLabel.contentMode = UIViewContentModeScaleAspectFill;
    cell.IMGLabel.image = nil;

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[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(), ^{
                        cell.IMGLabel.image = image;
                          [cell.IMGLabel setImage:image];
                          [cell.IMGLabel.layer setMasksToBounds:YES];
                          [cell.IMGLabel.layer setCornerRadius:2.5f];
                          [cell setNeedsLayout];

                });
            }
        }
    }];
    [task resume];

Your problem is that you have no guarantee that your NSUrlRequests will terminate in the same order than they started. 您的问题是您不能保证您的NSUrlRequests将以与它们开始时相同的顺序终止。 This is bad because your cells are re-used for better performance and it can end with strange behavior. 这很糟糕,因为您的单元格被重用以获得更好的性能,并且可能以奇怪的行为结束。

You can find a fix here: Asynchronous downloading of images for UITableView with GCD 你可以在这里找到一个修复: 使用GCD异步下载UITableView的图像

Or you can use tools listed here to help you address this issue: https://stackoverflow.com/a/32601838/3769338 或者您可以使用此处列出的工具来帮助您解决此问题: https//stackoverflow.com/a/32601838/3769338

暂无
暂无

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

相关问题 从 UITableView 单元格内的 url 加载异步图像 - 滚动时图像更改为错误图像 - Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling 从UITableView单元内的URL加载异步图像-滚动时图像错误 - Async image loading from url inside a UITableView cell - wrong image while scrolling 在此图像上执行某些操作后,将加载异步图像-滚动时图像变为错误的图像 - Async image loading after some actions on this image - image changes to wrong image while scrolling 从本地下载URL图像和uitableview单元图像加载,并在iOS中并行执行。 如何执行? - Downloading url image and uitableview cell image loading from local with parallel execution in iOS. how to implement? UITableView单元格显示错误的图像 - UITableView Cell showing wrong image 滚动时更改UITableView图像 - UITableView image changing while scrolling 在异步图像加载到 UITableViewCell 后滚动时,Swift 图像更改为错误的图像 - Swift Images change to wrong images while scrolling after async image loading to a UITableViewCell UITableView 的单元格从好图像闪烁到错误图像并在滚动后修复 - UITableView's cell is flickering from good to wrong image and fix after scrolling 带有图像的 UITableView 在滚动时显示错误的图像 - UITableView with images display wrong image for a moment while scrolling 在下载和保存文件时出现错误的CollectionView单元格图像,并使用completeBlock异步保存 - Wrong CollectionView cell image while downloading and saving file async with completionBlock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM