在我的应用程序中,我在 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/