如何在 uitableview ios6.1 中加载图像。
我目前正在使用SDWebImage https://github.com/rs/SDWebImage它可以工作。但有时它不会加载图像。
还有比我正在使用的库更好的库吗?我是 ios 的初学者,这对我有很大帮助。
提前致谢。
这是我现在使用的代码:
- (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] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
最佳答案
我将其放入答案中,因为评论太长:
SDWebImage 包的缓存功能非常强大 - 特别是磁盘缓存,这在 iOS 中可能有点麻烦(不同的操作系统版本支持不同级别/类型的缓存)。 SDWebImage 还提供了很多好的功能 - 您可以在其 GitHub 页面上查看要点(顶部的项目符号列表)。
如果它只是您想要的 UIImageView 类别(它提供了 setImageWithURL:placeholderImage:
方法),那么一些(我想说“大多数”,但我无法支持它)数据/统计)人们使用 AFNetworking,因为他们很可能已经将其用作网络堆栈。我们的生产应用程序使用 AFNetworking + SDWebImage,因为我们需要广泛的 AFNetworking 功能,而且还需要磁盘缓存。
哦,关于图像无法下载 - 可能是网络问题。事实上,您没有收到错误并且应用程序没有崩溃,这几乎是 SDWebImage 添加的功能的重点。实现类似的方法需要处理错误情况(网络错误、未经授权、错误的 URL),并在不阻塞主线程的情况下更新 UIImageView,所有这些实现起来都非常复杂,不仅对于初学者而且对于中级程序员来说也是如此。该功能看起来很简单 - 下载图像、使用占位符并在图像到达时进行更新 - 但这绝对是一项不简单的任务。
关于ios - 在tableview ios6.1中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15644647/