2017-02-01 45 views
0

我想將Facebook上的個人資料圖片上傳到Firebase。我試過這個答案:Upload Facebook image URL to Firebase Storage 但是,Swift給了我這個答案的第三行代碼錯誤。該代碼是:從URL獲取Facebook個人資料圖片

let dictionary = result as? NSDictionary 
let data = dictionary?.object(forKey: "data") 
let urlPic = (data?.objectForKey("url"))! as! String 

斯威夫特告訴我:不能調用非功能型的價值「任何?」之後我改變了代碼到什麼斯威夫特一直提示我:

let urlPic = ((data as AnyObject).object(ForKey: "url"))! as! String 

什麼是當我想要從Facebook的個人資料圖片使用的代碼?我的目標是將其存儲到Firebase中,但在我首次獲得個人資料圖片後纔會出現。

+0

你正在使用Swift 3? –

+0

您需要調用圖API。你用什麼來登錄Facebook的用戶? – dylanthelion

回答

0

答案是雨燕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") 
     } 


    } 
}) 
} 
相關問題