我正在使用一个iOS应用程序,在该应用程序中,我想拥有一个带有用户朋友的大图片的垂直桌面视图。(200x200px,100x100pt)我有效,但是超级慢!我几乎无法移动tableview!我该如何异步和方便地做到这一点?这是我当前的代码:在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:
- (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;
}
,如果您制作了自定义的uitableviewcell,则可以放置一个Uiview(而不是uiimageview),而在InterfaceBuilder中,则可以将其提供给fbprofilepictureview类。现在,您需要做的就是在该视图上设置ProfileID属性,Facebook SDK将处理提供带有提供的ID并显示的配置文件的个人资料图片。
Facebook SDK参考
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];
}
如果使用[nsdata datawithcontentsofurl:url],则图像将被获取同步。同样,每当您滚动表视图时,它们都会再次获取。
使用https://github.com/rs/sdwebimage异步图像加载。它也将缓存图像。
从egaimageView下载文件。此链接要求您下载文件。解开它。将这些文件添加到您的应用中。在您的班级中导入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];
}
希望,它可以帮助您。