使用3.0 SDK获取Facebook个人资料照片

bmu*_*ler 8 iphone facebook ios

我无法使用新的Facebook SDK获取用户的个人资料照片.这里的所有答案都使用旧SDK中不再有效的方法.

我已经尝试过使用Facebook教程中推荐的FBProfilePictureView,但是这张图片没有被缓存,我认为它不能转换成UIImage.

有人可以提供一些帮助吗?谢谢!

bmu*_*ler 14

好的,我终于使用以下内容得到了这个:

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");
Run Code Online (Sandbox Code Playgroud)

然后我使用-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data并将-(void)connectionDidFinishLoading:(NSURLConnection *)connection图像放在一起.


tri*_*777 9

我发现最好的解决方案:我使用过AFNetworking的UIImage https://github.com/AFNetworking/AFNetworking.它处理重定向和缓存,因此您可以为您拥有的每个用户ID获取个人资料图片.

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"]];
Run Code Online (Sandbox Code Playgroud)

  • +1为此.AFNetworking使它变得容易了10倍 (2认同)