nBackgroundWithBlock期间将个人资料图片上传到

nBackgroundWithBlock期间将个人资料图片上传到

本文介绍了在signUpInBackgroundWithBlock期间将个人资料图片上传到parse.com时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用下面的代码使用用户名,密码和个人资料图片来注册用户.

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{

    }
}

当我在parse.com上查看他的User类时,有一个文件已上传(请参见下面的屏幕截图)

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

尽管有一个文件,但是当我单击并下载它时,我将其打开以查找空白(即白色)图像.

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

profileAvatar是ViewController中的UIImageView,它确实指向有效的图像,因为运行应用程序时我可以看到它.

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:

推荐答案

在解析中,您需要先保存用户,然后保存图像,然后修改代码以实现以下目的:

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

这篇关于在signUpInBackgroundWithBlock期间将个人资料图片上传到parse.com时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:50