繁体   English   中英

如何使用SLRequest获取大尺寸的Facebook个人资料图片?

[英]How to get the facebook profile picture in large size using SLRequest?

我已经使用SLRequest获取个人资料信息,并且在iOS6中关注了获取用户个人资料图片? 获取个人资料图片。 但它返回50x50图片尺寸。 我需要大尺寸的头像。 谁能给我一些使用SLRequest进行操作的想法?

使用以下网址获取大图:

https://graph.facebook.com/fb_user_id/picture?type=large

对于封面图像,请使用以下内容:

http://graph.facebook.com/fb_user_id?fields=cover

这将返回:

{
  "cover": {
    "id": "XXX", 
    "source": "cover_image_url", 
    "offset_y": 66
  }, 
  "id": "XXXXXX"
}

您可以将个人资料图片放大为

[NSURL URLWithString:@"https://graph.facebook.com/me/picture?type=large"];

类型参数支持

enum{square,small,normal,large}

这是快速3的答案。

  1. 您必须使用accountStore API来访问其设备中用户的facebook帐户:

      func accessFacebookAccount(onCompletion: @escaping(_ tokenCredential: String, _ facebookAccount: ACAccount) -> Void, onError: @escaping (_ errorMessage: String?) -> Void) { let accountStore = ACAccountStore() let facebookAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook) let options = [ "ACFacebookAppIdKey" : "529210493918001", "ACFacebookPermissionsKey" : ["email","public_profile"], "ACFacebookAudienceKey" : ACFacebookAudienceOnlyMe] as [String : Any] accountStore.requestAccessToAccounts(with: facebookAccountType, options: options) { (granted, error) in if granted { let fbAccount = accountStore.accounts(with: facebookAccountType).last as? ACAccount let credential = fbAccount?.credential let token = credential?.oauthToken onCompletion(token!, fbAccount!) } else { let error = "Access to Facebook was not granted." onError(error) } } } 
  2. 获得响应后,用户便授予了对该帐户的访问权限。 您可以使用ACAccount对象并通过SLRequest访问Facebook信息:

     func getFacebookInfoViaAccounts (_ fbAccount: ACAccount) { let myURL = URL(string: "https://graph.facebook.com/me") let request = SLRequest.init(forServiceType: SLServiceTypeFacebook, requestMethod: .GET, url: myURL, parameters: ["fields":"email, first_name, last_name,birthday, gender"]) request?.account = fbAccount request?.perform(handler: { (dataResponse, urlResponse, error) in if error == nil { let json = try? JSONSerialization.jsonObject(with: dataResponse!, options: [ JSONSerialization.ReadingOptions.allowFragments]) if let dict = json as? [String: Any] { print("My data \\(dict)") } } }) let myPhotoURl = URL(string: "https://graph.facebook.com/me/picture") let requestPhoto = SLRequest.init(forServiceType: SLServiceTypeFacebook, requestMethod: .GET, url: myPhotoURl, parameters: ["type":"large"]) requestPhoto?.account = fbAccount requestPhoto?.perform(handler: { (dataResponse, urlResponse, error) in if error == nil { print("URL DE LA PHOTO \\(urlResponse?.value(forKey: "URL"))") } }) } 

上面的代码用于获取用户个人资料和信息(例如电子邮件,性别和生日)的图像。

如果用户的Facebook帐户已链接到他或她的iphone,则诸如tinder之类的应用程序就是通过这种方式获取您的数据,并且不要求用户登录到Facebook帐户。

我希望这对某人有所帮助,祝您编程愉快:)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM