2014-01-30 151 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進行異步圖像加載。它也會緩存圖像。

相關問題