Trouble in uploading profile image to parse.com during signUpInBackgroundWithBlock

132 views Asked by At

I am using the code below to sign up a user with username, password, and a profile picture.

func createUserwithAllFields() {
    let profileImageData =  UIImageJPEGRepresentation(profileAvatar.image, 0.6)
    //let profileImageData = UIImageJPEGRepresentation(profileImg, 0.6)
    let profileImageFile = PFFile(data: profileImageData)


    if tv_username.text != ""
        && tv_password.text != ""
    {

        var user = PFUser()

        user.username = tv_username.text
        user.password = tv_password.text

        user["profileImage"] = profileImageFile

        user.signUpInBackgroundWithBlock({ (success, error) -> Void in
            if error == nil {

                //if SignUp is successful

                let installation = PFInstallation.currentInstallation()
                installation["user"] = user
                installation.saveInBackgroundWithBlock(nil)

                //self.showChatOverview()

                self.goToMainScreen()

            } else {

            }
        })

    }else{

    }
}

When I look at he User class on parse.com there is a file that get's uploaded (see screenshot below) enter image description here

Although there is a file, when I click on it and download it, I open it to find a blank (i.e. white) image.

The profileAvatar is a UIImageView in the ViewController and it does point to a valid image because I can see that when I run the app.

This is the blank image that I see: enter image description here

1

There are 1 answers

10
Icaro On

In parse you need to first save the user and after save the image, I modify your code to accomplish that:

func createUserwithAllFields() {
    let profileImageData =  UIImageJPEGRepresentation(profileAvatar.image, 0.6)
    let profileImageFile = PFFile(data: profileImageData)
    if tv_username.text != "" && tv_password.text != "" {
        var user = PFUser()
        user.username = tv_username.text
        user.password = tv_password.text

        user.signUpInBackgroundWithBlock({ (success, error) -> Void in
            if error == nil {
                /println("SignUp is successful")
                 user["profileImage"] = profileImageFile
                 user.signUpInBackgroundWithBlock({ (success, error) -> Void in
                     if error == nil {
                         println("Image Saved")
                     } else {
                         println("Fail to Save Image")
                     }
                )}
            } else {
                println("Fail to Sign up")
            }
        })
    }else{
        println("Invalid username and password")
    }
}