2017-01-03 5 views
0

ParseクラスのUserテーブルにユーザーを追加する機能がありますが、サインアップフォームにアバターを追加します。Parse(Swift 3)を使用してユーザーにサインアップするときにアバターを追加するには

ストーリーボード側がうまくいき、カメラやフォトライブラリから画像を選択してイメージビュー(profilePic)に追加できますが、これをsignUpInBackgroundメソッドに追加しようとするとアプリがクラッシュします。

私はios開発の完全なnewbですが、他のコーディング言語に精通しているので完全に外国人ではありません。私がここで紛失しているかどうか、またはサインアップ時に画像を追加することはできません?

ヘルプ!

let user = PFUser() 

user.email = emailAddress.text 
user.username = screenName.text 
user.password = password.text 

let image = self.profilePic.image 

if image != nil { 

    let imagedata = UIImagePNGRepresentation(image!)! 

     let file = PFFile(name: "image.png", data: imagedata) 

     user["profilePic"] = file 

    } 

    user.signUpInBackground(block: { (success, error) in 

     if error != nil { 

      // error code 

     } else { 

      // user logged in successfully 

     } 
    }   
} 

答えて

0

あなたのできることは次のとおりです。私はそれにもバリデーションを加えました。また、私はそれを作成した後、あなたがそれをしたくない場合は削除することができます。これはswift3の例です!

@IBOutlet weak var avatarImage: UIImageView! 
@IBOutlet var emailAddress: UITextField! 
@IBOutlet var password: UITextField! 

// MARK: - UPLOAD AVATAR BUTTON 
@IBAction func uploadAvatarButt(_ sender: AnyObject) { 
    let alert = UIAlertController(title: APP_NAME, 
     message: "Select source", 
     preferredStyle: .alert) 

    let camera = UIAlertAction(title: "Take a picture", style: .default, handler: { (action) -> Void in 
     if UIImagePickerController.isSourceTypeAvailable(.camera) { 
      let imagePicker = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = .camera 
      imagePicker.allowsEditing = false 
      self.present(imagePicker, animated: true, completion: nil) 
     } 
    }) 

    let library = UIAlertAction(title: "Pick from library", style: .default, handler: { (action) -> Void in 
     if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 
      let imagePicker = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = .photoLibrary 
      imagePicker.allowsEditing = false 
      self.present(imagePicker, animated: true, completion: nil) 
     } 
    }) 

    // Cancel button 
    let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in }) 

    alert.addAction(camera) 
    alert.addAction(library) 
    alert.addAction(cancel) 
    present(alert, animated: true, completion: nil) 

} 

// ImagePicker delegate 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     avatarImage.image = image 
    } 
    dismiss(animated: true, completion: nil) 
} 

// MARK: - SIGNUP BUTTON 
@IBAction func signupButt(_ sender: AnyObject) { 

    if password.text == "" || emailAddress.text == "" || screenName.text == "" { 

      //You can alert here , that fields need to be filled. 

    } else { 
     let userForSignUp = PFUser() 
     userForSignUp.username = screenName.text!.lowercased() 
     userForSignUp.password = password.text 
     userForSignUp.email = emailAddress.text 

     if avatarImage.image != nil { 
      let imageData = UIImageJPEGRepresentation(avatarImage.image!, 0.8) 
      let imageFile = PFFile(name:"image.jpg", data:imageData!) 
      // profilePic needs to be the name of the col 
      userForSignUp["profilePic"] = imageFile 
     } 


     userForSignUp.signUpInBackground { (succeeded, error) -> Void in 
      if error == nil { 

       //Signup Success 
       PFUser.logInWithUsername(inBackground: self.screenName.text!, password:self.password.text!) { (user, error) -> Void in 
        if error == nil { 
         //Login Success 
        } else { 
         //Login Falied 
        }} 
      // ERROR 
      } else { 
       //Signup Failed 
     }} 
    } 
} 
+0

これは素晴らしいです!どうもありがとうございます!すべてのコードをすべて自分のものに置き換えるのではなく、何が違うのかを行ごとにチェックしたかったのですが、PNGではなくJpegを保存していたということだけが違っていました。出来た。これは、IOSのシミュレータのデフォルト画像のように、PNG形式で保存された場合、クラッシュの原因となった10MB以上です!これであなたの助けをありがとう! – Dan

+0

あなたはダン@ダンを歓迎します – Cliffordwh

関連する問題