如何在延迟加载中加载更多图像

Posted

技术标签:

【中文标题】如何在延迟加载中加载更多图像【英文标题】:How to load more images on Lazy Loading 【发布时间】:2013-03-12 08:42:11 【问题描述】:

在 mu UITable 视图中,我正在显示来自服务器的图像..为此我正在使用延迟加载。 当行在屏幕上时,我可以加载图像。如何增加加载的图像数量。

第一次看到 3 个表格单元格,这些单元格上的图像会延迟 2 秒加载。如何同时加载接下来 3 行的图像?

我正在使用下面的函数来加载图像

- (void)loadImagesForOnscreenRows
  
    if ([imageArray count] > 0)
    
    NSArray *visiblePaths = [tableView indexPathsForVisibleRows];
    for (NSIndexPath *indexPath in visiblePaths)
    
        AppRecord *appRecord = [dataArray objectAtIndex:indexPath.row];

        if (!appRecord.appIcon) // avoiding the imagen download if already has an image
        
            [self startIconDownload:appRecord forIndexPath:indexPath];
          
        
      
    

【问题讨论】:

为什么不用SDWebImage github.com/rs/SDWebImage 使用 SDWebImage。它非常快速且易于用于 tableview 中的图像 我已经下载了...但在我的 xcode 中无法使用 【参考方案1】:

你做事的方式

您可以通过在背景上下载图像来做到这一点,当它们完成后,您可以将NSNotification 发布到重新加载tableView 的方法中。但是在下载图像时,您必须向用户显示加载视图或其他内容,以告诉他图像正在下载。

建议

我不建议您这样做,最好的方法是在后台使用SDWebImage 独立下载它们,并更快地回复您的tableView。下载图像时,您会显示默认图像(占位符),下载图像后,此功能会将其设置在需要的位置:

SDWebImage

你只需要#import <SDWebImage/UIImageView+WebCache.h>到你的项目,你也可以在下载图片时定义占位符,只需要这个代码:

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

        static NSString *CellIdentifier;
        CustomCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        MyObject *object = (MyObject *)[self.list.array objectAtIndex:indexPath.row];
        
        //Here is where you set your image URL and it's placeholder
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        
        return cell;

它还缓存下载的图像并为您提供出色的性能。

【讨论】:

【参考方案2】:

您为什么要使用此代码,您只需将其更改为低于键值,它就可以完美地处理不受限制的图像。

kIDStr     = @"id";  
kNameStr   = @"im:name";  
kImageStr  = @"im:image";  
kArtistStr = @"im:artist";  
kEntryStr  = @"entry";  

【讨论】:

以上是关于如何在延迟加载中加载更多图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在页面加载时从表中加载一定数量的行,并且仅在用户加载它们时才加载更多行?

在颤振加载中加载更多列表,但用户界面没有改变

如何使用 JSON API 在滚动时在 recyclerview 中加载更多数据

在 pyqt 中加载图像和文本

在 WordPress 中加载更多帖子 Ajax 按钮

如何在UITableView SWIFT中加载更多单元格