bmueller
bmueller

Reputation: 2701

Get Facebook profile pic using 3.0 SDK

I'm having trouble getting the user's profile pic with the new Facebook SDK. All the answers on here use methods from the old SDK that no longer work.

I've tried using the FBProfilePictureView recommended in Facebook's tutorial, but this picture doesn't get cached and I don't think it can be converted into a UIImage.

Can anyone please provide some help? Thanks!

Upvotes: 8

Views: 9474

Answers (5)

trickster77777
trickster77777

Reputation: 1208

Best solution i've found: I've used UIImage from AFNetworking https://github.com/AFNetworking/AFNetworking. It handles redirect and caching so you can fetch profile picture for every user id you have.

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"]];

Upvotes: 8

Mehul Solanki
Mehul Solanki

Reputation: 463

If your problem is related to "self.userPofilePicture.profileID = user.id;".

Then self.userProfilePicture return UIView so just take View and display in it.

Don't forgot add class FBProfilePictureView in Identity Inspector as well as [FBProfilePictureView class] to AppDelegate implementation file.

I hope this will helpful.

Upvotes: 0

Minh Phan
Minh Phan

Reputation: 49

Use https://github.com/rs/SDWebImage to cache the image. The URL to request the image should be

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

Upvotes: 1

bmueller
bmueller

Reputation: 2701

OK, I finally got this using the following:

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");

Then I used -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data and -(void)connectionDidFinishLoading:(NSURLConnection *)connection to put the image together.

Upvotes: 13

jhoanna
jhoanna

Reputation: 1857

Use the link:

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

to get the current user's profile picture.

Facebook provides the Graph API explorer tool which can come in handy when you want to try queries or to check the returned output.

Here's a link to the User API Reference.

Upvotes: 0

Related Questions