0

Ich bekomme Facebooks Profilbild und zeige es als Profilbild in meiner App an. Hier ist der Code.Verwende Facebook Profilbild als dein Profilbild Swift

if let user = FIRAuth.auth()?.currentUser{ 
    let photoUrl = user.photoURL 
    let name = user.displayName 

    self.FacebookUser.text = name 

    let storage = FIRStorage.storage() 

    //refer your particular storage service 
    let storageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com") 
    let profilePicRef = storageRef.child(user.uid+"/profile_pic.jpg") 

    profilePicRef.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) -> Void in 
     if (error == nil){ 

      self.FacebookPic.image = UIImage(data: data!) 

     }else{ 

      print("Error downloading image:") 


     } 
    }) 

     if(self.FacebookPic.image == nil) 
    { 
     var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height": 300, "width": 300, "redirect": false], httpMethod: "GET") 
     profilePic?.start(completionHandler: {(_ connection, result, error) -> Void in 
      // Handle the result 


      if error == nil { 
       if let dictionary = result as? [String: Any], 
        let data = dictionary["data"] as? [String:Any], 
        let urlPic = data["url"] as? String{ 

        if let imageData = NSData(contentsOf: NSURL(string: urlPic)!as URL){ 

         let uploadTask = profilePicRef.put(imageData as Data, metadata: nil) { 
          metadata, error in 

          if (error == nil) 
          { 
           let downloadurl = metadata!.downloadURL 
          } 
          else 
          { 
           print("Error in downloading image") 
          } 
         } 

         self.FacebookPic.image = UIImage(data: imageData as Data) 
        }}}})} 
}else{ 
} 

//The END of the Facebook user and picture code 

konnte ich es für ein paar Tage zum Laufen bringen und es funktioniert jetzt nicht mehr, ich habe von durchgestrichenen Linie weg und ich kann ehrlich nicht, warum es nicht funktioniert.

+0

sicher, dass Sie das Bild auf die Aktualisierung Haupt-Bedroung. wenn Sie das tun: 'self.FacebookPic.image = UIImage (data: data!)' – Scriptable

+0

@Scriptable wie mache ich das? – juelizabeth

Antwort

0

ich diesen Code verwendet:

func pictureFromFirebase(loginMethod: Int) 
{ 
    if loginMethod == 0 //FB 
    { 
     var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height":300, "width":300, "redirect":false], httpMethod: "GET") 
     let profilePicRef = storageRef.child((user?.uid)!+"/profile_pic.jpg") 
     profilePicRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in 
      if let error = error { 
       // Uh-oh, an error occurred! 
       // but we don't need to do anything yet. Try to download the profile pic 
      } 
      if (data != nil) 
      { 
       print("no need to download image from facebook") 
       self.profileImage.image = UIImage (data: data!) 
      } 
      else 
      { 
       // THIS IS THE BLOCK THAT HAS BEEN MOVED 
       // WHICH WILL NOW BE EXECUTED IN TWO CONDITIONS - 
       // 1. AN ERROR IN THE DOWNLOAD 
       // 2. NO PROFILE PIC AVAILABLE 
       print("downloading image from facebook") 
       profilePic?.start(completionHandler: {(_ connection, _ result, _ error) -> Void in 
        if (error == nil) 
        { 
         if let dictionary = result as? [String:Any], let data = dictionary["data"] as? [String:Any], 
          let urlPic = data["url"] as? String { 
          if let imageData = NSData(contentsOf: NSURL(string: urlPic)! as URL) 
          { 
           let uploadTask = profilePicRef.put(imageData as Data, metadata: nil){ 
            metadata, error in 
            if (error == nil) 
            { 
             let downloadUrl = metadata!.downloadURL 
            } 
            else 
            { 
             print("error in downloading image") 
            } 
           } 
           self.profileImage.image = UIImage(data: imageData as Data) 
          } 
         } 

        } 
       }) 
      } 

     } 
    } 
} 

von diesem Posten Second If statement gets called before first statement finished in one function und es funktionierte

0

Sie erhalten nur Ihre facebook Profil Bild. mit dieser URL und stellen Sie die URL in Ihren UIImageView

let profilepicURl = "https://graph.facebook.com/\(user_id_fb)/picture?type=large" //user_id_fb like 1251246454544 your facebook ID 
Verwandte Themen