【发布时间】:2013-07-15 22:47:06
【问题描述】:
我有一个显示 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滚动非常流畅。
看起来 profilePic 是最差的。这是因为来自 Facebook 的数据请求。我已将 profilePic 更改为仅不是 URL 请求的默认 Pic,并且表格视图很好。所以这显然是 setImageWithURL 的性能问题。
看看其他问题,这似乎是实现这一目标的最佳方式:- Best practices for managing facebook friends pics in iOS App
欢迎讨论
【问题讨论】:
-
头像有多大?
-
如果删除 profilePic 会怎样?是不是更快?
-
它可能会有所帮助,setImageWithURL: 来自 AFNetworking 库,它正在异步加载图像:engineering.gowalla.com/AFNetworking/Categories/…:
-
@PatrickTescher checkout graph.facebook.com/martin-magakian/picture 请求的图片很小
-
@MartinMagakian 如果你想使用它 Facebook 有自己的头像查看类:developers.facebook.com/docs/reference/ios/3.5/class/… 它会自动选择合适的图片下载,管理 url 连接等。
标签: ios ios5 uitableview afnetworking