ios - 每次滚动后 tableview 中的图像都会发生变化

标签 ios multithreading uitableview asynchronous

您好,我是 IOS 的新手,所以请原谅愚蠢的问题。我正在开发一个在 tableview 中加载大图像的应用程序。我正在异步获取本地镜像并存储到字典中以进行缓存 问题是当我滚动 tableview 图像更改和调整大小时有滞后,有时如果我添加更多图像并滚动很多时间应用程序本身会崩溃 这是我的代码:

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

    UITableViewCell *cell =
        (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell =
            [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                   reuseIdentifier:CellIdentifier];
    }
      NSError *attributesError = nil;

    NSString *workSpacePath = [[self applicationDocumentsDirectory]
                            stringByAppendingPathComponent:[images objectAtIndex:indexPath.row]];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager]
                                 attributesOfItemAtPath:workSpacePath error:&attributesError];
    NSNumber * fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
    long long int fileKBSize = [fileSizeNumber longLongValue]/1024;

   cell.textLabel.backgroundColor = [UIColor clearColor];
    NSString *descriptionString =  [images objectAtIndex:indexPath.row];
    NSLog(@"descriptionString :%@",descriptionString);
    if ([[despDectionary objectForKey:descriptionString] length] > 0) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[despDectionary objectForKey:descriptionString]];
    }
    else {
            cell.detailTextLabel.text = @"File Description";
    }

    if([self.cacheImages objectForKey:descriptionString]!=nil)
    {
     cell.imageView.image = [self.cacheImages valueForKey:descriptionString];
    }

    else
    {
    dispatch_queue_t bg = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(bg,^{

    UIImage *myimage=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];

   NSData *imageData = UIImageJPEGRepresentation(myimage, 0.2);
    UIImage *image = [UIImage imageWithData:imageData];
    UIImage *thumb = [image makethumbnail:CGSizeMake(110, 70)];
    dispatch_async(dispatch_get_main_queue(), ^{
   // [cell.setNeedsLayout];
     cell.imageView.frame = CGRectMake(0, 0, 110, 70);
    if([tableView indexPathForCell:cell].row== indexPath.row)
    {
    //cell.imageView.frame = CGRectMake(0, 0, 110, 70);
    NSString *imagename = [NSString stringWithFormat:@"%@",[despDectionary objectForKey:descriptionString]];
     [self.cacheImages setValue:thumb forKey:imagename];
           cell.imageView.frame = CGRectMake(0, 0, 110, 70);
      NSLog(@"cacheImages -- %@",self.cacheImages);
    cell.imageView.image = [self.cacheImages valueForKey:imagename];
   // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
      });
    });
  }
  // NSLog(@" Dictionary CACHE IMAGES is @%",self.cacheImages);
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 0, 49,56);
    [button setImage:[UIImage imageNamed:@"deletButotn.png"] forState:UIControlStateNormal];
    button.tag = indexPath.row;
    [button addTarget:self action:@selector(deleteButtonAction:)  forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];


    return  cell;
} 

最佳答案

我建议使用 SDWebimage 来管理这个...如此简单且一致。

https://github.com/rs/SDWebImage

关于ios - 每次滚动后 tableview 中的图像都会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092218/

相关文章:

ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK

javascript - 如何在网站的移动设备上录制音频?

ios - 了解函数中的快速代码元组复合

json - 使用 JSON 的 API 到 TableView

ios - 如何将图像放在 UIActionSheet 上?

Java:即使使用 SwingUtilities.invokeLater,线程也不会修改 GUI

java - 将 Swing/FX 与绑定(bind)混合 - 使用自定义属性在线程之间进行调解?

java - java中如何停止或杀死一个线程?

ios - 当 Web 服务中没有可用的新数据时,如何在 iOS 中停止 Spinner 动画?

ios - 如何防止 UITableViewCell 的背景 View 在点击时被清除?