iOS Facebook Graph请求多种图片尺寸

时间:2015-10-25 02:26:40

标签: ios facebook swift facebook-graph-api

我正在使用Facebook的图形API,并试图获得用户当前个人资料图片的两种不同图片大小。一张图片我希望尺寸为250x250,另一张图片我希望尺寸为1080x1080。这是我目前的代码:

    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)
        }

    }

这会将URL返回到大小为1080x1080的图片并且有效。现在,如果我更改参数以添加对大小为250x250的图片的请求:

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

我得到此错误基本上说我不能多次使用字段picture

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)

这是否意味着实现我想要的唯一方法是制作batch request?如果是这种情况,是否有一个函数在批处理请求完成后被调用?

3 个答案:

答案 0 :(得分:3)

使用field aliasing进行一次非批处理请求时,还有另一种(更好)方法可以做到这一点,我最近遇到同样的问题时遇到过。

在OP的情况下,fields参数将设置为:

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

然后,响应将包括名为picture_small和picture_large的两个字段,其中包含相应的图片数据。 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"
}

答案 1 :(得分:3)

这对我来说可以获得大尺寸的图像。检查以下查询。 NSString * graphPath = [NSString stringWithFormat:@"%@?fields=photos.fields(id,link,source)", albumID];

答案 2 :(得分:2)

您有两种解决方案:

无聊一个是提出两个请求。而更好的解决方案是提出批量请求:

这是我的个人资料的cURL批量请求,它有效:

  

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

您只需要放置访问令牌。