Dustin Spengler
Dustin Spengler

Reputation: 7731

Download high resolution profile image Facebook Graph API

Using Swift 3.0, I can download the current users image using this graph call function:

fileprivate func getCurrenUserHighResImageURL() -> String? {
    var photoURLOutput : String?
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "picture.type(large)"])
    graphRequest.start(completionHandler: { connection, result , error -> Void in

        if ((error) != nil) {
            print("Error: \(error)")
        } else {
            let castedResult = result as? [String : AnyObject]
            if let castedResult = castedResult {

                if let photURL = castedResult["picture"] as? [String : AnyObject] {
                    let photoData = photURL["data"] as? [String : AnyObject]
                    photoURLOutput = photoData?["url"] as? String
                    if let photoURLOutput = photoURLOutput {
                        CURRENT_USER_URL.updateChildValues(["highResImageURL" : "\(photoURLOutput)"])
                    }
                }
            }
        }
    })
    return photoURLOutput
}

However this only returns a 200 x 200 pixel image. Is there any way to make a graph call to to a higher resolution?

Ive seen people making calls to the graph API using a URL like this: https://graph.facebook.com/userId/picture?width=640&height=640

as mentioned in this post : Facebook Graph Profile Picture Link

But when i attempt to download an image from that URL...

 func loadUserImage(fromURL urlString:String?) {
    if urlString != nil {
        let imgURL: URL = URL(string: urlString!)!
        let request: URLRequest = URLRequest(url: imgURL)

        let session = URLSession.shared
        let task = session.dataTask(with: request, completionHandler: {
            (data, response, error) -> Void in

            if (error == nil && data != nil) {
                func display_image() {
                    let userImage = UIImage(data: data!)
                    self.userImage.image = userImage
                }
                DispatchQueue.main.async(execute: display_image)
            }
        })
        task.resume()
    }
}

there is no image to be found. (I know this function works because it works on all other images). When I type the link into my browser directly it get a JSON error back saying i don't have authorization.

Has anyone had any luck make a graph call like this? Is there some syntax I have overlooked which will return a higher res profile image?

Upvotes: 1

Views: 1743

Answers (1)

Jawwad
Jawwad

Reputation: 1336

This is what I have to request 200x200 on an iPhone 7 and 300x300 on a plus size. However I don't think you get an image back with those exact sizes. It might be slightly larger.

let deviceScale = Int(UIScreen.main.scale)
let width = 100 * deviceScale
let height = 100 * deviceScale
let parameters = ["fields": "first_name, last_name, picture.width(\(width)).height(\(height))"]

In summary, the syntax for the param to request a 400x400 would be picture.width(400).height(400).

Upvotes: 4

Related Questions