2012-08-29 142 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

相关问题