How to get profile picture from facebook

I am trying to get a user profile picture. But since Facebook is updating its sdk API to version 4.0 or later, as well as on the FB developers site, they explain in Objective-C, but I want the code in Swift. I do not understand how to use FBSDKProfilePicturein my code. I need help getting a user profile picture.

I want to save the image in a second view. Please help me with the code. Here is my code.

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

let loginManager = FBSDKLoginManager()
let loginView : FBSDKLoginButton = FBSDKLoginButton()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        println("user already logged in")
        loginManager.logOut()
        // User is already logged in, do work such as go to next view controller.
    }

    self.view.addSubview(loginView)
    loginView.center = self.view.center
    loginView.readPermissions = ["public_profile", "email", "user_friends"]
    loginView.delegate = self
    FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKProfileDidChangeNotification, object: nil)

}

func returnUserData()
{
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("fetched user: \(result)")
            let userName : NSString = result.valueForKey("name") as! NSString
            println("User Name is: \(userName)")
            let userEmail : NSString = result.valueForKey("email") as! NSString
            println("User Email is: \(userEmail)")
    }
    })
}

}

+4
source share
2 answers

If you can log in using Facebook, you must have FB IDthis user. For this purpose, you can use this to get a profile image:

func getProfPic(fid: String) -> UIImage? {
    if (fid != "") {
        var imgURLString = "http://graph.facebook.com/" + fid! + "/picture?type=large" //type=normal
        var imgURL = NSURL(string: imgURLString)
        var imageData = NSData(contentsOfURL: imgURL!)
        var image = UIImage(data: imageData!)
        return image
    }
    return nil
}

type URL.

+12

: .

http://graph.facebook.com/[your_fbid]/picture?type=large&redirect=true&width=500&height=500

func getProfPic(fid: String) -> UIImage? {
    if (fid != "") {
        var imgURLString = "http://graph.facebook.com/" + fid! + "/picture?type=large&redirect=true&width=500&height=500" //replace 500 with your desired size
        var imgURL = NSURL(string: imgURLString)
        var imageData = NSData(contentsOfURL: imgURL!)
        var image = UIImage(data: imageData!)
        return image
    }
    return nil
}
0

All Articles