简体   繁体   中英

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

I have used the SLRequest for getting profile info and i followed the Getting user profile picture in iOS6? to get the profile image. But it returning 50x50 image dimension. I need large size of profile picture. Can anyone give me some idea to do this using SLRequest ?

use the below URL to get the large image:

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

For cover image, use the below:

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

this will return:

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

You can get the large image of profile picture as

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

The type parameter supports

enum{square,small,normal,large}

Here is the answer for swift 3.

  1. you have to get the access to the facebook account of the user in his or her device, this using accountStore API:

      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. Once you get the response, and the user granted the access to the account. You can make use of the ACAccount object and access the facebook info via a SLRequest:

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

Above is the code for getting the image of the users profile and the info, like email, gender and birthday.

This is the way apps like tinder get your data and dont request the user to log in to the facebook account, if the user has his facebook account linked to his or her iphone.

I hope this helps somebody, happy coding :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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