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



我有一个函数,可以成功地将用户添加到我的 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
        }
    }        
}

这是你可以做的。我也为它添加了验证。我还创建了它,一旦成功注册,请登录用户。如果您不想要它,可以将其删除。 这是一个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
        }}
    }
}

最新更新