2012-08-29 136 views
8

我無法使用新的Facebook SDK獲取用戶的個人資料圖片。這裏的所有答案都使用舊SDK中不再有效的方法。使用3.0 SDK獲取Facebook個人資料圖片

我試過使用Facebook教程中推薦的FBProfilePictureView,但這張圖片沒有緩存,我不認爲它可以轉換成UIImage。

任何人都可以請提供一些幫助嗎?謝謝!

回答

14

OK,我終於用下面得到這個:

imageData = [[NSMutableData alloc] init]; // the image will be loaded in here 
NSString *urlString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", user.id]; 
NSMutableURLRequest *urlRequest = 
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] 
            cachePolicy:NSURLRequestUseProtocolCachePolicy 
           timeoutInterval:2]; 

// Run request asynchronously 
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest 
                       delegate:self]; 
if (!urlConnection) 
     NSLog(@"Failed to download picture"); 

然後我用-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data-(void)connectionDidFinishLoading:(NSURLConnection *)connection把圖像在一起。

+0

我通過NSURLConnection * urlConnection獲取FB Profile圖像,但請讓我知道如何在UIImageview上實現此功能 – nivritgupta

0

使用鏈接:

https://graph.facebook.com/me?fields=picture

來獲取當前用戶的資料圖片。

Facebook提供了Graph API explorer tool,當您想要嘗試查詢或檢查返回的輸出時,它可以派上用場。

這是鏈接到User API Reference

+1

我該如何從該鏈接去獲取它緩存在我的應用程序? – bmueller

1

使用https://github.com/rs/SDWebImage來緩存圖像。請求圖片的網址應爲

NSString *string = [NSString stringWithFormat:@"https://graph.facebook.com/%@?fields=picture", userid]; 
+0

將URL更改爲後爲我工作:NSString * string = [NSString stringWithFormat:@「https:// graph .facebook.com /%@/picture「,userid];' – smek

0

如果您的問題與「self.userPofilePicture.profileID = user.id;」有關。

然後self.userProfilePicture返回UIView所以只需要查看並顯示它。

不要忘記在Identity Inspector中添加class FBProfilePictureView以及將[FBProfilePictureView類]添加到AppDelegate實現文件。

我希望這會有所幫助。

+0

我認爲這是爲了和FBLoginView –

9

我找到的最佳解決方案: 我使用了AFNetworking的UIImage https://github.com/AFNetworking/AFNetworking。它處理重定向和緩存,因此您可以爲每個用戶標識獲取配置文件圖片。

NSString *imageUrl = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", ((NSDictionary<FBGraphUser> *) [self.friends objectAtIndex: indexPath.row]).id]; 
[friendCell.imageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile.jpg"]]; 
+2

+1一起使用。 AFNetworking使它更容易10x –

+0

什麼是self.friends? –

+0

fb用戶數組 – trickster77777

相關問題