2017-09-23 16 views
3
var img: UIImage! 

if self.profileImg.image == #imageLiteral(resourceName: "add_image"){ 
    img = #imageLiteral(resourceName: "add_image") 
}else{ 
    img = self.profileImg.image 
} 

if let imgData = UIImageJPEGRepresentation(img, 0.2){ 
    print(imgData) 
    let imageUid = NSUUID().uuidString 
    print(imageUid) 
    let metaData = StorageMetadata() 
    print(metaData) 
    metaData.contentType = "image/jpeg" 

    DataService.ds.REF_PROFILE_IMAGES.child(imageUid).putData(imgData, metadata: metaData, completion: { (metadata, error) in 
     if error != nil { 
      print ("ADAM: UNABLE TO UPLOAD IMAGE TO FIREBASE STORAGE") 
     }else{ 
      print ("ADAM: SUCCESSFULLY UPLOADED PROFILE IMAGE ") 
      self.downloadURL = metaData.downloadURL()?.absoluteString 


     } 
    }) 
}else{ 
    print("ADAM: SUMTING WONG") 
} 

Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!) { (user, error) in 

    if error == nil { 
     print("ADAM: You have successfully signed up") 

     // saving the image to storage and link to database 

     let firstName = self.firstNameField.text 
     let lastName = self.LastNameField.text 
     let userName = self.usernameField.text 
     let profileImageUrl = self.downloadURL 
     print(profileImageUrl) 
     let userData = ["provider" : user!.providerID, "FirstName": firstName, "LastName": lastName, "username": userName, "profileImageURL": profileImageUrl] 

     self.completeSignIn(id: user!.uid, userData: userData as! Dictionary<String, String>) 

プロファイル画像URLは繰り返しnilを返すため、サインイン機能を実行できません。私がデバッグすると、Dataserviceメソッドがスキップされ、私は理由を理解できないことがわかります。/イメージをfirebaseストレージにアップロードしようとしていますが、実行されていませんか?

+1

あなたはnilを見ている、あなたはこれをチェックすることができますhttps://stackoverflow.com/question/46356012/firebase-one-of-two-observers-not-working/46356267#46356267 – 3stud1ant3

+0

@ 3stud1ant3私はputData補完ハンドラの中でユーザを作成しようとしましたが、 – KONADO

答えて

2

私はこれを使用してユーザーを作成し、ユーザー情報をFirebaseデータベースとストレージに保存します。あなたはputData`閉鎖 `内側` createUser`メソッドを呼び出すか、完了ハンドラを使用するか必要があるので、 `putData`は、非同期呼び出しであるため、

func registerUser(withName: String, email: String, password: String, profilePic: UIImage, completion: @escaping (Bool) -> Swift.Void) { 
     Auth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in 
      if error == nil { 
       user?.sendEmailVerification(completion: nil) 
       let storageRef = FIRStorage.storage().reference().child("usersProfilePics").child(user!.uid) 
       let imageData = UIImageJPEGRepresentation(profilePic, 0.1) 
       storageRef.putData(imageData!, metadata: nil, completion: { (metadata, err) in 
        if err == nil { 
         let path = metadata?.downloadURL()?.absoluteString 
         let values = ["name": withName, "email": email, "profilePicLink": path!] 
         FIRDatabase.database().reference().child("users").child((user?.uid)!).child("credentials").updateChildValues(values, withCompletionBlock: { (errr, _) in 
          if errr == nil { 
           let userInfo = ["email" : email, "password" : password] 
           UserDefaults.standard.set(userInfo, forKey: "userInformation") 
           completion(true) 
          } 
         }) 
        } 
       }) 
      } 
      else { 
       completion(false) 
      } 
     }) 
    } 
関連する問題