使用Parse注册用户时如何添加头像(Swift 3)

时间:2017-01-03 17:33:41

标签: ios swift parse-platform

我有一个成功将用户添加到Parse类中的User表的函数,但是我想在注册表单中添加一个头像。

事情的stoyboard方面工作正常,我可以从相机或照片库中选择和成像并将其添加到我的imageView(profilePic)但是当我尝试将其添加到signUpInBackground方法时,它会崩溃应用程序。

我是ios开发的完全新手,但我熟悉其他编码语言,所以它并非完全是外来的,我只是不知道我在这里缺少什么,或者是否只能在注册时添加图像?

帮助!

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

        }
    }        
}

1 个答案:

答案 0 :(得分: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
        }}
    }
}