如何从Facebook获取个人资料图片
how to fetch profile picture from facebook
我正在尝试获取用户的头像。但由于 Facebook 已将其 API sdk 更新到 v4.0 或更高版本,并且在 FB 开发者网站上,他们在 Objective C 中进行了解释,但我想要 Swift 中的代码。我不知道如何在我的代码中使用 FBSDKProfilePicture
。我需要帮助来获取用户的个人资料照片。
我想将图片存储在我的第二个视图中。请任何人帮助我编码。
这是我的代码。
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)")
}
})
}
}
如果您能够使用 Facebook 登录,那么您应该拥有该用户的 FB ID
。有了它,您可以使用它来获取个人资料图片:
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
}
您还可以在 URL
中将 type
参数更改为不同的大小。
获取自定义尺寸的图片:添加宽度和高度参数以及您需要的尺寸。
喜欢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
}
我正在尝试获取用户的头像。但由于 Facebook 已将其 API sdk 更新到 v4.0 或更高版本,并且在 FB 开发者网站上,他们在 Objective C 中进行了解释,但我想要 Swift 中的代码。我不知道如何在我的代码中使用 FBSDKProfilePicture
。我需要帮助来获取用户的个人资料照片。
我想将图片存储在我的第二个视图中。请任何人帮助我编码。 这是我的代码。
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)")
}
})
}
}
如果您能够使用 Facebook 登录,那么您应该拥有该用户的 FB ID
。有了它,您可以使用它来获取个人资料图片:
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
}
您还可以在 URL
中将 type
参数更改为不同的大小。
获取自定义尺寸的图片:添加宽度和高度参数以及您需要的尺寸。
喜欢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
}