简体   繁体   English

TableViewCells中的iOS Facebook SDK个人资料图片

[英]iOS Facebook SDK Profile Pictures in TableViewCells

I'm working on an iOS application where I want to have a vertical tableview with large-ish pictures of a user's friends. 我正在开发一个iOS应用程序,在该应用程序中我想使用垂直视图查看用户朋友的大图片。 (200x200px, 100x100pt) I have it working, but it is super slow! (200x200px,100x100pt)我可以使用,但是速度太慢了! I can barely move the tableview at all! 我几乎无法移动tableview! How can I do this asynchronously and in an expedient manner? 如何才能以异步方式方便地做到这一点? This is my current code: In viewDidload: 这是我当前的代码:在viewDidload中:

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: 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;
}

It might be easier for you if you made a custom UITableViewCell, then you can place a UIView (instead of UIImageView) and in InterfaceBuilder give it the class FBProfilePictureView. 如果您创建了一个自定义UITableViewCell,可能对您来说比较容易,那么您可以放置​​一个UIView(而不是UIImageView),并在InterfaceBuilder中为其提供类FBProfilePictureView。 Now, all you need to do is set the profileID property on that view, and the Facebook SDK will handle getting the profile picture for the profile with the provided id and displaying it. 现在,您需要做的就是在该视图上设置profileID属性,Facebook SDK将处理获取具有提供的ID的配置文件的配置文件图片并显示它。

Facebook SDK Reference Facebook SDK参考

The UIImageView+AFNetworking class from AFNetworking Framework is one of the most used methods to do this. AFNetworking Framework中的UIImageView + AFNetworking类是执行此操作的最常用方法之一。 You can easy import this Framework to your project: 您可以轻松地将此框架导入到您的项目中:

https://github.com/AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking

And use like this: 像这样使用:

#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];
}

If you use [NSData dataWithContentsOfURL:url] the images will be fetched synchronous. 如果使用[NSData dataWithContentsOfURL:url],则将同步获取图像。 Also when ever you scroll the table view they will be fetched again. 同样,当您滚动表格视图时,也会再次获取它们。

Use https://github.com/rs/SDWebImage for asynchronous image loading. 使用https://github.com/rs/SDWebImage进行异步图像加载。 It will also cache the images. 它还将缓存图像。

Download the files from EgoImageView . EgoImageView下载文件。 This link ask you to download a file. 此链接要求您下载文件。 Unzip it. 解压缩。 Add those file into your app. 将这些文件添加到您的应用中。 Import EgoImageView.h in your class. 在您的课程中导入EgoImageView.h Change this code instead of your code. 更改此代码而不是您的代码。 First time, it will take time to load. 第一次加载需要时间。 If you are converting URL as data, it will take long time. 如果要将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];
}

Hope, it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM