Ios 滚动时,TableView单元格运行缓慢

Ios 滚动时,TableView单元格运行缓慢,ios,ios5,uitableview,afnetworking,Ios,Ios5,Uitableview,Afnetworking,我有一个tableView,其中显示了Facebook好友的列表。我在单元格中有一个imageview,它显示用户的profilePicture。使用AFNetworking,我调用新图像并在加载时放置占位符 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"

我有一个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出现一些速度/性能问题。有人知道为什么会这样吗?这个设置正确吗

单元是使用故事板创建的原型单元。我还为单元格中的其他对象编写了其他代码,但是删除profile Picture部分会使单元格正常运行,所以这似乎是问题所在,只是不确定原因

编辑

UIImage *defaultPhoto = [UIImage imageNamed:@"person40x40.png"];
NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=40&height=40", friend[@"id"]];
我已更新图片以适应40x40图像视图,但没有任何差异。iphone4加载桌面单元格的速度很慢。与iphone5相比,iphone5的滚动非常流畅

以下是最重的时间分析器:

看来这是最糟糕的。这是因为Facebook的数据请求。我已经将profilePic更改为默认Pic,它不是一个URL请求,表视图也可以。因此,这显然是setImageWithURL的性能问题

从其他问题来看,这似乎是实现这一目标的最佳途径:


任何受欢迎的想法

自动布局都可能导致一些问题。此外,如果在图像进入时调整其大小,则可能会导致速度变慢。确定的最简单方法是运行时探查器(选中“隐藏系统库”和“仅显示Obj-C”)。这将告诉你你的慢是从哪里来的。

我现在没有什么办法来改善你的性能问题

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)网络请求改进1/2
阅读一些关于和的帖子

让我们试试看。在其他线程中调用图像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)网络请求改进2/2 我只是想找到另一种方法。而且它可能更可靠。
从苹果自己下载。它的创建正是为了教授您所面临的问题。


最后,我认为setImageWithURL:可能没有那么快(?)而且N°1点也可能是您的问题的一部分。查看苹果只需使用NSURLConnection即可。

个人资料图片有多大?如果删除个人资料图片怎么办?更快吗?这可能会有帮助,setImageWithURL:来自AFNetworking库,它正在异步加载图像:@PatrickTescher checkout请求的图片很漂亮small@MartinMagakian如果你想使用它,Facebook有自己的个人资料图像视图类:它会自动选择合适的图像下载,管理url连接等。
隐藏系统库
提示在查看时间分析器时非常有用。