滚动时TableView单元格很慢

时间:2013-07-15 22:47:06

标签: ios ios5 uitableview afnetworking

我有一个tableView,显示了Facebook好友的列表。我在单元格中有一个imageview,显示用户的profilePicture。使用AFNetworking我调用新图像并在加载时放置占位符。

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

// Configure the cell...
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSDictionary *friend = (NSDictionary *)[self.searchResults objectAtIndex:indexPath.row];
        cell.textLabel.text = [friend valueForKey:@"name"];
    } else {
        NSDictionary *friend = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:10];
        UILabel *displayName = (UILabel *)[cell.contentView viewWithTag:20];
        UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];

        displayName.text = [friend valueForKey:@"name"];

        UIImage *defaultPhoto = [UIImage imageNamed:@"person.png"];
        NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture", friend[@"id"]];
        NSURL *avatarUrl = [NSURL URLWithString:urlString];
        [profilePic setImageWithURL:avatarUrl placeholderImage:defaultPhoto];
        profilePic.contentMode = UIViewContentModeScaleAspectFit;
}

这会导致tableview出现一些速度/性能问题。有没有人知道为什么会这样,这个设置是否正确?

单元格是使用故事板创建的原型单元格。我还有其他对象在单元格中的其他代码,但删除配置文件图片部分使单元格正常执行所以这似乎是问题,只是不知道为什么。

修改

UIImage *defaultPhoto = [UIImage imageNamed:@"person40x40.png"];
NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=40&height=40", friend[@"id"]];

我更新了图片以适应40x40图片视图,但没有区别。 iphone4加载表格单元格很慢。与滚动时非常流畅的iphone5相比。

以下是Time Profiler中最重的: enter image description here

看起来profilePic是最糟糕的。这是因为来自Facebook的数据请求。我已经将profilePic更改为仅作为URL请求的默认Pic,并且表视图很好。所以这显然是setImageWithURL的性能问题。

看看其他问题,它似乎是实现这一目标的最佳方式: - Best practices for managing facebook friends pics in iOS App

欢迎任何想法

2 个答案:

答案 0 :(得分:0)

自动布局可能会导致一些问题。此外,如果您在图像进入时调整图像大小,则可能会导致图像变慢。最简单的方法是运行Time Profiler(隐藏系统库和仅显示Obj-C)。这将告诉你缓慢的来源。

答案 1 :(得分:0)

我现在几乎没有想过要改善你的性能问题。

1)请求您的朋友改进

从您的编辑看起来就像请求您的朋友也花了相当多的时间:

NSDictionary *friend = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

我确信您可以使用以下内容缓存此调用的大部分内容:

-(void)viewDidLoad{
    [super viewDidLoad];
    this.friendsInsensitive = [self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}



2)Web请求改进1/2
阅读关于NSURLConnection and the event loopblocking the main thread with async call

的帖子

试试吧。在另一个帖子中调用图像Web请求。

[...]
    UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:10];
    UILabel *displayName = (UILabel *)[cell.contentView viewWithTag:20];
    UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];

    displayName.text = [friend valueForKey:@"name"];

    UpdateImgContainter *containter = [UpdateImgContainter alloc] initWithCell:cell andFriendId:friend[@"id"];

    [self performSelectorInBackground:@selector(requestImg:) withObject:containter];
}


-(void)requestImg:(UpdateImgContainter *)containter{
     UIImage *defaultPhoto = [UIImage imageNamed:@"person.png"];
     NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture", containter.friendId];
     NSURL *avatarUrl = [NSURL URLWithString:urlString];
     [profilePic setImageWithURL:avatarUrl placeholderImage:defaultPhoto]; //warning it's not good practice to upload the UI from an other thread than MainThread
     profilePic.contentMode = UIViewContentModeScaleAspectFit;
}



2)Web请求改进2/2 我只想找到另一种方法。它可能更可靠。
从Apple本身下载此LazyTableImages project。正是为了教你正面临的问题而创造的
在一天结束时,我认为setImageWithURL:可能不是那么快(?)而且N°1点也可能是你问题的一部分。查看LazyTableImages project Apple只需使用NSURLConnection。