Ios 下载缩略图图像后重新加载UItableViewCell
我有一个应用程序,在tableViewCell中下载图像,然后将其设置为缩略图。我的问题是,我看到缩略图刷新的唯一方法是单击另一个选项卡,然后返回到tableViewCell所在的选项卡。下面是设置缩略图的代码Ios 下载缩略图图像后重新加载UItableViewCell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个应用程序,在tableViewCell中下载图像,然后将其设置为缩略图。我的问题是,我看到缩略图刷新的唯一方法是单击另一个选项卡,然后返回到tableViewCell所在的选项卡。下面是设置缩略图的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"username"];
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
PFFile *thumbnail = nil;
if (thumbnailArray.count > 0){
thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
dispatch_async(backgroundQueue, ^{
NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];
[[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
}
return cell;
}
我建议您使用AFNetworkings类别UIImage+AFNetworking,它允许您只需执行[imageView setImageWithURL:(NSURL)],即可在后台管理所有内容。您不必担心加载图像、缓存图像或诸如此类的事情。它将为您缓存它 问题是
@property(strong)NSMutableDictionary*imagesDictionary代码>并实例化它self.imagesDictionary=[NSMutableDictionary]代码>
在索引路径行的单元格中,在下载缓存之前检查缓存是否存在
PFFile *thumbnail = nil;
if (thumbnailArray.count > 0)
{
//If image is present in cache load from cache
UIImage *image = [self.imagesDictionary objectForKey:indexPath];
if (image)
{
[cell.imageView setImage:image];
}
else
{
//If Image is not present fire a download
thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
dispatch_async(backgroundQueue, ^{
NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];
//Cahce it to your image dictionary
[self.imagesDictionary setObject:[UIImage imageWithData:thumbnailData] forKey:indexPath];
//Reload the dictionary
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
}
下面的代码帮助解决了我的问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"username"];
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
PFFile *thumbnail = nil;
if (thumbnailArray.count > 0){
thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
dispatch_async(backgroundQueue, ^{
NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
[tableView reloadInputViews];
});
});
}
return cell;
}
问题是重新加载TableView是在一个单独的线程上调用的。一旦我将图像的设置移动到主线程并从那里调用ReloadInputView,我的问题就解决了 [需要单元格布局]代码>我已放置[单元格布局需要];在声明单元格变量之后,我的问题仍然存在。您建议我将该方法放在何处?您正在后台线程上更新UI。大禁忌。将其分派到主线程。我放入Dispatch_sync以获取主线程,然后调用[talbeView reloadInputViews];它起作用了。改用SDWebImage。