我在分组的 UITableView 中有一些大图像。每个部分中有 1 行保存图像,并且关于图像的每个部分都有关联的页眉和页脚文本。我努力让它在iPhone OS 3.x上平滑滚动,终于成功了。但是……当我升级到 iOS 4 后,一切都改变了。现在,每当加载新单元时,性能就会非常不稳定。这是我的 cellForRowAtIndexPath 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
ApplicationDelegate *appDelegate = (ApplicationDelegate *)[[UIApplication sharedApplication] delegate];
NSString *fileName = [NSString stringWithFormat:@"%@",[[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainTrackImage"]];
UIImage *theImage = [UIImage imageNamed:[[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainTrackImage"]];
imageHeight = CGImageGetHeight(theImage.CGImage);
imageWidth = CGImageGetWidth(theImage.CGImage);
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, (imageHeight*320)/imageWidth)] autorelease];
photo.tag = PHOTO_TAG;
[cell addSubview:photo];
} else {
photo = (UIImageView *) [cell viewWithTag:PHOTO_TAG];
[photo setFrame:CGRectMake(0, 0, 320, (imageHeight*320)/imageWidth)];
}
photo.image = theImage;
return cell;
}
注意:我尝试使用以下代码代替“imageNamed”方法,但没有成功:
UIImage *theImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath,fileName]];
我什至尝试过预先缓存图像,但没有成功。 任何帮助将不胜感激。
更新: 我尝试过使用 Grand Central Dispatch 通过创建图像队列并异步加载图像来加载图像:
dispatch_async(_image_queue, ^{
photoImageView.image = [imageCacheNS objectForKey:imageString];});
我遇到的问题是图像加载速度非常慢,当我滚动表格时,之前查看的图像会显示几秒钟,直到加载新图像。
更新2: 我仍然无法完成这项工作。我现在正在研究使用 drawRect 方法来绘制单元格。我希望这会提高滚动性能。 Apple 的 tableViewSuite 示例代码示例 #5 展示了如何执行此操作,但我无法找到如何使此功能适用于我的项目。有人有使用drawRect:将图像加载到tableview中的经验吗?
谢谢!
最佳答案
尝试在另一个线程中加载图像。这将有助于提高表格 View (和其他 UI 元素)的响应能力。在这里, block 和 libdispatch 会成为代码的绝佳补充。
关于iphone - iOS 4 升级后 UITableView 的滚动性能出现抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125346/