gpt4 book ai didi

ios - 使用多线程为 UITableViewCell 下载图像,但在滚动 UITableView 之前无法加载图像

转载 作者:行者123 更新时间:2023-11-29 10:37:38 26 4
gpt4 key购买 nike

我正在尝试使用多线程在 UITableViewCell 中下载图像。但是,当我使用模拟器查看结果时,在滚动表格 View 之前无法加载图像。

看了很多StackOverFlow的例子和教程,还是一点用都没有。实际上,我从 Flickr 服务器下载图像并将它们存储到缓存字典中。但是我仍然可以第一次加载图像,除非我滚动表格 View 并且图像开始出现。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld", (long)indexPath.row];

NSDictionary *photo = self.recentPhotos [indexPath.row];
NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE];

cell.textLabel.text = title;
[cell.textLabel setFont:[UIFont fontWithName:@"OpenSans" size:18]];

if ([self.cashedImages objectForKey:cellIdentifier] != nil) {
cell.imageView.image = [self.cashedImages objectForKey:cellIdentifier];
}
else
{
dispatch_queue_t queue = dispatch_queue_create("fetch photos", 0);
dispatch_async(queue, ^{

NSURL *imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatSquare];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

dispatch_async(dispatch_get_main_queue(), ^{
if ([tableView indexPathForCell:cell].row == indexPath.row) {
[self.cashedImages setValue:image forKey:cellIdentifier];
cell.imageView.image = [self.cashedImages valueForKey:cellIdentifier];
}
});
});
}
return cell;
}

最佳答案

对于此任务,最好使用库。通常我使用非常易于使用的 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 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;
}

编辑:

如果您想先下载所有图像并缓存它们,您不应该在 tableviewCell 中处理它。 TableViewCells 将在创建时开始下载 -> 当它们变得可见时。

SDWebImageView 兑现之前下载的图片,所以尝试在 viewDidLoad 方法中添加这段代码:

for (NSURL* url in self.recentPhotos) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];

[manager downloadImageWithURL:url options:SDWebImageContinueInBackground progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
//Your code if needed
}];
}

SDWebImage 根据图片的绝对 url 路径缓存图片。

关于ios - 使用多线程为 UITableViewCell 下载图像,但在滚动 UITableView 之前无法加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26115357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com