How to get photos of a facebook album in iPhone SDK?

落花浮王杯 提交于 2019-11-28 21:56:35

So /me/albums returns an array of Album objects, each of which has an id.

If an album had an id of 99394368305, you can go to

https://graph.facebook.com/99394368305/photos

to get an array of photo objects.

Each of these will have the picture and source properties to get the image data from facebook.

They will all have an images array as well if you want pre-scaled images instead of just the originals.


All the facebook queries give JSON back - I'm assuming that you know how to parse this?

Engr Irfan Ali

It takes lot of time because of following steps;

1) get uid of friend using :

NSString *urlString=[NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@&fields=id,picture,name,birthday",fbGraph.accessToken];

2) put friends uid to get album id:

NSString *urlString1=[NSString stringWithFormat:@"https://graph.facebook.com/%@/albums?access_token=%@",obj.uid,fbGraph.accessToken];

NSString *album = [[[_response1 objectForKey:@"data"]objectAtIndex:j]objectForKey:@"name"];

if ([album isEqualToString:@"Profile Pictures"]) 
{
      albumid = [[[_response1 objectForKey:@"data"]objectAtIndex:j]objectForKey:@"id"];
}

3) then put the album id to get the picture from that album:

NSString *albumUrl=[NSString stringWithFormat:@"https://graph.facebook.com/%@/photos?type=album&access_token=%@",albumid,fbGraph.accessToken];

 NSString *picUrl=[[[[[_response2 objectForKey:@"data"]objectAtIndex:0]objectForKey:@"images"]objectAtIndex:0]objectForKey:@"source"];

Kindly tell me some other method to get profile picture with large size in a single step to save time.

The code below should do the right job for you. It gets albums for logged user and then requests for photos of this albums. In the place where is a comment you will have the access to all photos. For more info you can refer to docs:

FBRequest *request = [FBRequest requestForGraphPath:@"me/albums"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary <FBGraphUser> *my, NSError *error) {
    if ([my[@"data"]count]) {
        for (FBGraphObject *obj in my[@"data"]) {
            FBRequest *r = [FBRequest requestForGraphPath:[NSString stringWithFormat:@"/%@/photos", obj[@"id"]]];
            [r startWithCompletionHandler:^(FBRequestConnection *c, NSDictionary <FBGraphUser> *m, NSError *err) {
                //here you will get pictures for each album
            }];
        }
    }

}];

Facebook developers new code:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                               initWithGraphPath:@"/{user-id}"
                                      parameters:params
                                      HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    // Handle the result
}];

Latest Facebook Graph API docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!