我想将个人资料图片从Facebook上传到Firebase。我试过这个答案:Upload Facebook image URL to Firebase Storage 但是,Swift在这个答案的第三行代码上给了我错误。代码是:
let dictionary = result as? NSDictionary
let data = dictionary?.object(forKey: "data")
let urlPic = (data?.objectForKey("url"))! as! String
斯威夫特告诉我:不能称之为非功能类型的价值'任何?!'在我将代码更改为Swift一直建议我的内容之后:
let urlPic = ((data as AnyObject).object(ForKey: "url"))! as! String
当我想从Facebook检索个人资料图片时使用的代码是什么?我的目标是将其存储到Firebase中,但是在我首先获得个人资料图片之后,这将会出现。
答案 0 :(得分:0)
答案在 Swift 1.2
我参考了here并实施了
你可以这样做:
// accessToken is your Facebook id
func returnUserProfileImage(accessToken: NSString)
{
var userID = accessToken as NSString
var facebookProfileUrl = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")
if let data = NSData(contentsOfURL: facebookProfileUrl!) {
imageProfile.image = UIImage(data: data)
}
}
这是我获得Facebook ID的方式:
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)")
if let id: NSString = result.valueForKey("id") as? NSString {
println("ID is: \(id)")
self.returnUserProfileImage(id)
} else {
println("ID es null")
}
}
})
}