【发布时间】:2012-12-31 11:59:47
【问题描述】:
我必须下载 20-25 个 50 Kb- 2 Mb 的图像,并在表格视图中显示它们。 我对此使用了 ASIHTTPRequest 异步请求。我观察到一段时间后应用程序卡住了。这不应该发生,因为我正在使用异步调用。我认为 ASIHTTPRequest 有问题,我观察到 didFinished 选择器在主线程中被调用。我唯一能做的就是
-(void)didFinishedDownloadingImage:(ASIHTTPRequest*)request { NSData *responseData = [请求响应数据]; UIImage *image = [UIImage imageWithData:responseData]; [[data objectAtIndex:request.tag] setImage:image]; [self.tableView reloadData]; }我认为这不会造成任何问题。同样在 cellforrowatindexpath 我只是这样做
- (UItableViewCell *)tableviewView:(UItableView *)tableview cellForItemAtIndexPath:(NSIndexPath *)indexPath { UserProfile * 用户 = [数据 objectAtIndex:indexpath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:@"ProfileCell" forIndexPath:indexPath]; 如果(细胞 == 零){ 单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewDefaultStyle]; } NSString *fullname = [NSString stringWithFormat:@"%@\n%@", 用户名,用户名,用户名]; if(user.image != nil) [cell.imageView setImage:user.image]; 别的{ [cell.imageView setImage:[UIImage imageNamed:@"placeholder.jpg"]]; } [cell.label setText:fullname]; 返回单元格; }但是应用程序很慢并且会冻结 1-2 秒,这是相当长的时间。 我见过可以非常顺利地执行此操作的应用程序。我尝试使用固定大小的 5Kb 图像,使用上述代码可以显着提高性能。我不知道为什么在这种情况下这会对大图像产生影响,因为所有下载都是通过 ASIHTTP 在其他线程中发生的。
【问题讨论】:
-
[self.tableView reloadData]是一个非常昂贵的操作。选择性地重新加载您需要的单元格。 -
@Srikanth 如何将图像转换为缩略图??
-
@JackLawrence ,我怀疑 setImage 需要时间。因为即使向上或向下滚动,我也会看到滞后。 (滚动表格视图时调用重新加载单元格)如果我一次加载一个或整个表格没有任何区别。
标签: iphone ios objective-c asihttprequest