V1P3R
V1P3R

Reputation: 176

How to save images to Parse?

For some odd reason, my images have stopped saving to parse. Before, my code worked fine and I have not made any changes to it at all.

Here is my code:

 var posts = PFObject(className: "Product")
            posts["shortDescription"] = productShortDescription
            posts["user"] = PFUser.currentUser()
            posts["longDescription"] = productLongDescription
            posts["title"] = productTitle
            posts["price"] = productPrice
            posts.saveInBackgroundWithBlock({
                (success: Bool, error: NSError?) -> Void in

                if error == nil {
                    //success saving, now save image

                    //create image data

                    var imageData = UIImagePNGRepresentation(self.newItemImageView.image)


                    //create parse file

                    var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
                    posts["imagePNG"] = parseImageFile
                    posts.saveInBackgroundWithBlock({
                        (success: Bool, error: NSError?) -> Void in

                        if error == nil {
                            // take user home
                            println("data uploaded")
                            self.performSegueWithIdentifier("returnHomeAfterUpload", sender: self)
                        }else {
                            println(error)

                        }


                    })

                }else {

                    println(error)
                }

            })

Everything else is stored perfectly, but what's the issue with my image data? Thanks!

Upvotes: 2

Views: 1702

Answers (2)

Adnan Aftab
Adnan Aftab

Reputation: 14487

In your code you are not saving parseImageFile, so first parseImageFile.SaveInBackground and on success set it to posts and then save posts as well

Should be something like this

         var posts = PFObject(className: "Product")
                posts["shortDescription"] = productShortDescription
                posts["user"] = PFUser.currentUser()
                posts["longDescription"] = productLongDescription
                posts["title"] = productTitle
                posts["price"] = productPrice
                posts.saveInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in

                    if error == nil {
                        //success saving, now save image
                        //create image data
                        var imageData = UIImagePNGRepresentation(self.newItemImageView.image)                            
                        //create parse file
                        var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
parseImageFile.saveInBackgroundWithBlock({
posts["imagePNG"] = parseImageFile
                        posts.saveInBackgroundWithBlock({
                            (success: Bool, error: NSError?) -> Void in

                            if error == nil {
                                // take user home
                                println("data uploaded")
                                self.performSegueWithIdentifier("returnHomeAfterUpload", sender: self)
                            }else {
                                println(error)

                            }


                        })
})


                    }else {

                        println(error)
                    }

                })

I haven't test this code on editor you may find some syntax error, but it should be something like this...

so things is when you create parseImageFile and then saveInBackground and inside block set it posts and then save post again

Upvotes: 2

vsilux
vsilux

Reputation: 790

I've checked your code and it's works fine.

        let image = UIImage(named: "img.jpg")
        let data = UIImagePNGRepresentation(image)
        let file = PFFile(name: "img", data: data)

        let parseObj = PFObject(className: "testClass")
        parseObj["text"] = "hello"
        parseObj["image"] = file

        parseObj.saveInBackgroundWithBlock { (_, _) -> Void in }

Try this, if this code will be works - easiest way for you is remove "Product" table and create it again.

Upvotes: 1

Related Questions