簡體   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