【发布时间】:2013-08-02 02:55:58
【问题描述】:
我在我的UITableViewCell 中异步加载UIImageViews。我的图像视图是 100 点乘 100 点的正方形,但我检索到的 UIImages 并不总是具有相同的宽度和高度。
我的问题是:在加载单元格时,UIImageViews 都是 100 x 100,这很好,但内部 UIImages 被拉伸到那个大小(左图)。当我按下单元格并突出显示时,图像会突然调整到正确的比例(右图)。此外,当我进一步向下滚动到UITableView 时,所有图像也是 100x100 并被拉伸,但是当我向上滚动再次查看单元格时,它们已经突然调整大小了。不过,我不知道他们遵循的缩放比例。
我做错了什么?我已经将UIImageViews'contentMode 设置为UIViewContentModeCenter,但正如您所见,它不起作用。请帮忙?我的完整代码如下。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchResultCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchResultCell"];
}
// Get the Ad object that will be displayed in this cell.
Ad *ad = (Ad *)[self.searchResults objectAtIndex:indexPath.row];
// Set up the detail text on the right.
cell.textLabel.attributedText = ad.attributedTextDisplay;
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
// Set up the image on the left.
NSString *thumbnailURL = ad.thumbnailURL;
NSData *data = [FTWCache objectForKey:[MD5 hash:thumbnailURL]];
// If the image has been previously downloaded and is stored in FTWCache.
if (data) {
// Then use that data instead.
cell.imageView.image = [UIImage imageWithData:data];
} else {
// Assign default image before beginning background image retrieval task.
cell.imageView.image = [UIImage imageNamed:@"image_stub_ad"];
// Trigger background task that retrieves the actual thumbnail from the URL.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailURL]];
// Proceed only with updating the table view cell if the returned NSData is not nil.
if (imageData) {
dispatch_sync(dispatch_get_main_queue(), ^{
// UPDATED: Added call to setNeedsDisplay.
UITableViewCell *theSameCell = [tableView cellForRowAtIndexPath:indexPath];
theSameCell.imageView.image = [UIImage imageWithData:imageData];
[theSameCell setNeedsDisplay];
});
}
});
}
// UPDATED: Set the contentMode to UIViewContentModeScaleAspectFit instead of UIViewContentModeCenter.
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
【问题讨论】:
-
在您下面的声明中“// 将 contentMode 设置为 ...”:
[tableView cellForRowAtIndexPath].imageView.contentMode = UIViewContentModeCenter;单元格可能仍然是nil- 这是委托的全部目的:返回一个单元格该索引路径。您可以将其更改为:cell.imageView.contentMode = UIViewContentModeCenter; -
而不是这个
[tableView cellForRowAtIndexPath:indexPath].imageView.contentMode = UIViewContentModeCenter;使用这个cell.imageView.contentMode = UIViewContentModeScaleAspectFit; -
^谢谢你们两个,我就是这么做的。但是,当图像缺少宽度时,它们仍然会在突出显示/滚动时向左移动。但是,对于缺少高度的图像,情况并非如此,只要它们的宽度可以填满整个 100 x 100 区域。我现在要拔头发了……
-
一个问题是
[tableView cellForRowAtIndexPath:indexPath]返回的nil单元格,另一个与视图模式有关。但是,从远程获取图像数据后,您必须在完成处理程序中调用[tableView cellForRowAtIndexPath:indexPath]! -
cell.imageView.image = [UIImage imageWithData:imageData];
标签: iphone ios objective-c asynchronous uiimageview