我有 tableviewcontroller 和从服务器获取的数据。我使用以下类来下载异步数据。但我的问题是当用户看到 tableViewcontroller 时正在加载数据。我希望在用户看到之前加载数据。
#import <SDWebImage/UIImageView+WebCache.h>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.textLabel.font = [UIFont fontWithName:@"BebasNeue" size:24];
cell.textLabel.textColor = [UIColor whiteColor];
NSString *imageURLString=[[tableData objectAtIndex:indexPath.row] valueForKey:@"logo"];
NSString* imageURL = [[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"];
[cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]];
}
Solution
With SDWeb图像 https://github.com/rs/SDWebImage您可以先下载图像。
Where?
这取决于您的实施。也许在以前的控制器中,或者在您的appDelegate
。如果tableview
出现在你的首字母中viewcontroller
,机会绝对取决于网络速度。
示例代码
看看这段代码(从库的Readme中提取,检查来源!):
Manager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// YOU DON'T NEED THIS
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
// MAYBE YOU WANT TO DO SOMETHING HERE
}
}];
然后,在你的tableView:cellForRowAtIndexPath:
方法,你可以像这样设置图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
NSURL* url = GET_IMAGE_URL;
[cell.imageView setImageWithURL:url];
// ...
}
怎么运行的?
该库首先负责在其缓存中查找,在那里它将找到之前下载的图像,因为它使用图像 url 作为缓存的键,以便图像立即出现。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)