0

我有一个显示 Facebook 好友列表的 tableView。我在单元格中有一个图像视图,它显示用户的 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 中最重的: 在此处输入图像描述

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

看看其他问题,这似乎是实现这一目标的最佳方式:-在 iOS 应用程序中管理 facebook 朋友图片的最佳实践

欢迎任何想法

4

2 回答 2

0

自动布局可能会导致一些问题。此外,如果您在图像进入时调整它们的大小,这可能会导致速度变慢。最简单的确定方法是运行 Time Profiler(选中 Hide System Libraries 和 Show Obj-C Only)。这会告诉你你的缓慢来自哪里。

于 2013-07-16T03:19:35.783 回答
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 和事件循环的帖子并使用异步调用阻塞主线程

让我们尝试一下。在另一个线程中调用图像 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 项目。它的创建正是为了教授您面临的问题。

归根结底,我只是认为 setImageWithURL: 可能没有那么快(?),而第 N°1 点可能也是您问题的一部分。查看LazyTableImages 项目Apple 只需使用 NSURLConnection。

于 2013-07-19T02:24:34.917 回答