首页 文章

iOS Facebook Graph请求多种图片尺寸

提问于
浏览
2

我正在使用Facebook的图形API,并试图获得用户当前 Profiles 图片的两种不同图片大小 . 一张图片我希望尺寸为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的图片并且可以正常工作 . 现在,如果我更改params以添加对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 回答

  • 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的两个字段,其包含相应的图片数据 . 例如:

    {
      "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"
    }
    
  • 2

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

  • 3

    你有两个解决方案:

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

    这是我的 Profiles 的cURL批量请求,它有效:

    curl -XPOST“https://graph.facebook.com/v2.4”-d“access_token = put_your_access_token_here”-i -d'batch = [{“method”:“GET”,“relative_url”:“我? 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

    您只需要放置访问令牌即可 .

相关问题