Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 320
bmueller Avatar asked Aug 29 '12 03:08

bmueller


3 Answers

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.

like image 113
bmueller Avatar answered Oct 01 '22 16:10

bmueller


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"]];
like image 33
trickster77777 Avatar answered Oct 01 '22 14:10

trickster77777


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];
like image 34
Minh Phan Avatar answered Oct 01 '22 15:10

Minh Phan