Thomas
Thomas

Reputation: 2386

iOS Facebook Graph Request multiple picture sizes

I'm using Facebook's graph API and am trying to get two different picture sizes of a user's current profile picture. One picture I would like to be size 250x250, and the other picture I would like be size 1080x1080. Here is my current code:

    let params = ["fields": "first_name, last_name, email, picture.width(1080).height(1080)"]
    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
    graphRequest.startWithCompletionHandler { (connection, result, error) in
        if error != nil {
            print(error)
        }

    }

This returns the URL to a picture of size 1080x1080 and works. Now if I change the params to add a request for a picture of size 250x250:

let params = ["fields": "first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)"]

I get this error basically saying I can't use the field picture more than once:

Syntax error "Field picture specified more than once. This is only possible before version 2.1" at character 94: first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)

Does this mean the only way to accomplish what I want is to make a batch request? And if that's the case, is there a function that gets called once both batch requests have completed?

Upvotes: 3

Views: 1188

Answers (3)

Rahul Bansal
Rahul Bansal

Reputation: 1530

This worked for me to get the large size images. Check the following query. NSString * graphPath = [NSString stringWithFormat:@"%@?fields=photos.fields(id,link,source)", albumID];

Upvotes: 3

Andy
Andy

Reputation: 561

There's another (better) way of doing this with just a single, non-batch request by using field aliasing, which I came across recently when I had the same issue.

In the OP's case the fields parameter would be set to:

first_name, last_name, email, picture.width(250).height(250).as(picture_small), picture.width(1080).height(1080).as(picture_large)

The response will then include two fields named picture_small and picture_large containing the respective picture data. E.g:

{
  "first_name": "Mark",
  "last_name": "Zuckerberg",
  "picture_small": {
    "data": {
      "height": 320,
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/p320x320/12208495_10102454385528521_4749095086285673716_n.jpg?oh=e14ffdec03ceb6f30da80d1c4c5c4c02&oe=57254837",
      "width": 320
    }
  },
  "picture_large": {
    "data": {
      "height": 547,
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/12208495_10102454385528521_4749095086285673716_n.jpg?oh=b008ae35daeea9b0d9babbee9d701a34&oe=572336EC",
      "width": 547
    }
  },
  "id": "4"
}

Upvotes: 7

William Kinaan
William Kinaan

Reputation: 28799

You have two solutions:

the boring one is to make two requests. while the better solution is to make a batch request:

This is the cURL batch request to my profile, and it works:

curl -XPOST "https://graph.facebook.com/v2.4" -d "access_token=put_your_access_token_here" -i -d 'batch=[{"method" : "GET", "relative_url":"me?fields=first_name,last_name,picture.width(250).height(250)"},{"method" : "GET", "relative_url" : "me?fields=first_name,last_name,picture.width(1080).height(1080)"}]' -i

You just need to put your access token.

Upvotes: 2

Related Questions