2014-01-30 98 views
1

我正在一个iOS应用程序,我想有一个垂直桌面与用户的朋友的大图片。 (200x200px,100x100pt) 我有它的工作,但它超级慢!我根本无法移动桌面视图! 我怎样才能以异样的方式做到这一点? 这是我当前的代码: 在viewDidLoad中:iOS中的Facebook SDK个人资料图片在TableViewCells

FBRequest* friendsRequest = [FBRequest requestForMyFriends]; 
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, 
               NSDictionary* result, 
               NSError *error) 
{ 
    self.friends = [result objectForKey:@"data"]; 
    NSLog(@"Found: %u friends", (unsigned)self.friends.count); 
    [self.leftTableView reloadData]; 
    [self.rightTableView reloadData]; 
}]; 

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (!cell) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 

NSDictionary<FBGraphUser> *friend = [self.friends objectAtIndex:indexPath.row]; 
if (friend) 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?width=200&height=200", friend.id]]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    UIImage *image = [UIImage imageWithData:data]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame = CGRectMake(0, 4, 100, 100); 
    imageView.layer.cornerRadius = 50.0f; 
    imageView.clipsToBounds = YES; 
    [cell.contentView addSubview:imageView]; 
} 

return cell; 
} 
+1

您之前使用过AFNetworking吗? – sathiamoorthy

回答

0

EgoImageView下载的文件。此链接要求您下载文件。解压缩。将这些文件添加到您的应用中。在你的班级中导入EgoImageView.h。更改此代码而不是您的代码。第一次加载需要时间。如果您将URL转换为数据,则需要很长时间。

if (friend) 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?width=200&height=200", friend.id]]; 
    UIImageView *imageView = [[UIImageView alloc] init]; 
    [imageView setImageURL:url]; 
    imageView.frame = CGRectMake(0, 4, 100, 100); 
    imageView.layer.cornerRadius = 50.0f; 
    imageView.clipsToBounds = YES; 
    [cell.contentView addSubview:imageView]; 
} 

希望,它可以帮助你。

1

来自AFNetworking Framework的UIImageView + AFNetworking类是最常用的方法之一。您可以轻松导入此框架到您的项目:

https://github.com/AFNetworking/AFNetworking

而且使用这样的:

#import "UIImageView+AFNetworking.h" 

NSDictionary<FBGraphUser> *friend = [self.friends objectAtIndex:indexPath.row]; 
if (friend) 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?width=200&height=200", friend.id]]; 

    UIImageView *imageView = [[UIImageView alloc] init]; 
    imageView.frame = CGRectMake(0, 4, 100, 100); 
    imageView.layer.cornerRadius = 50.0f; 
    imageView.clipsToBounds = YES; 
    [imageView setImageWithURL:url]; 
    [cell.contentView addSubview:imageView]; 
} 
3

,如果你做了一个自定义的UITableViewCell,那么你可以把一个UIView这可能是你更容易(而不是UIImageView),并在InterfaceBuilder中为其提供FBProfilePictureView类。现在,您需要做的就是在该视图上设置profileID属性,Facebook SDK将使用提供的ID获取配置文件的配置文件图片并显示它。

Facebook SDK Reference

1

如果使用[NSData的dataWithContentsOfURL:URL]的图像将获取同步。同样,当你滚动表格视图时,它们将被再次取出。

使用https://github.com/rs/SDWebImage进行异步图像加载。它也会缓存图像。

相关问题