在signUpInBackgroundWithBlock

时间:2015-06-10 02:50:56

标签: ios swift parse-platform uiimage

我正在使用以下代码向用户注册用户名,密码和个人资料照片。

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{

    }
}

当我在parse.com上查看他User课时,有一个上传的文件(见下面的截图) enter image description here

虽然有一个文件,当我点击它并下载它时,我打开它以找到一个空白(即白色)图像。

profileAvatar是ViewController中的UIImageView,它确实指向了有效的图片,因为我可以在运行应用时看到它。

这是我看到的空白图片: enter image description here

1 个答案:

答案 0 :(得分:0)

在解析时,您需要先保存用户,保存图像后,我会修改您的代码以实现:

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")
    }
}