ios - 异步加载uitableview中的照片

标签 ios multithreading uitableview

在我的应用程序中,我在 cellforrowatindexpath 中加载单元格的照片,但它需要主线程很长时间,并且当我滚动时,我的应用程序会停止一段时间。 我应该怎么办?如果我添加异步线程,它会在我每次滚动时调用它,并且可能有 2 个或更多相同的照片加载请求,这并不好......没有意大利面条编码有什么想法吗?

最佳答案

试用 SDWebImage 框架

https://github.com/rs/SDWebImage

示例代码

- (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];
    }

    // Here we use the new provided sd_setImageWithURL: method to load the web image
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"Get each url from array and set it here using indexpath.row"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

它支持各种功能,如缓存等。

关于ios - 异步加载uitableview中的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750783/

相关文章:

ios - Swift 中的 cURL 等价物 - iOS

ios - 从应用商店安装后是否可以获取链接内容?

python - 与线程一起使用时,使用 pywinauto.top_window() 会挂起

java - 线程名称和两次调用run方法

ios - 如何在不更改文本的 alpha 的情况下更改 TableViewCell 的 alpha?

ios - 带有 fetchedResultsController 的 Tableview 在应用重新启动或 View 重新加载之前不会更新新内容

ios - 在 TableView 中更改图像按钮

ios - 如何从 iOS 应用程序向 Facebook 好友发送应用程序邀请

ios - Core Image 会立即加载图像数据吗?

c++ - 使线程按顺序重做打印功能