无法使用Swift更新Parse中的imagefile列

时间:2016-07-30 16:53:37

标签: swift parse-platform ios9 xcode7.3

我在表视图中选择一个单元格,将值传递给视图控制器,然后允许用户使用下面的代码更新Parse上的行(在Heroku上运行)。

我正面临的问题是 - “标题”列已更新,但图像文件列未更新所选的新图像。我可以看到新图像确实传递到代码中。

我正在使用同步调用,因为除非保存记录,否则不希望用户继续前进。

什么可能是错的?保存新对象的相同代码工作正常。

由于 阿希什

                let query = PFQuery(className:"class")
                query.whereKey("objectId", containsString: passedObject.objectId)

                do {

                    let results = try query.findObjects() 

                    if results.count == 0 {

                         print("error")

                        success = false

                     } else {
                        //update


                        do {

                            let obj = results.first

                            obj!["title"] = Title.text!

                            let imageData = UIImageJPEGRepresentation(imageOutlet.image!,0.2)
                            let imageFile = PFFile(name: "image.png", data: imageData!)


                            //try imageFile?.save() // is this needed?
                            obj!["imagefile"] = imageFile

                            try obj!.save()
                            self.success = true 


                        }  catch let er as NSError {

                            print(" error while updating - \(er)")
                        }

                     }
                } catch {

                    print("error while querying  \(error)")

                    success = false
                }

1 个答案:

答案 0 :(得分:1)

我在我身边进行了一些测试,这段代码对我有用:

Aync版本

    let query = PFQuery(className: "FileTest")
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        let imageData = UIImageJPEGRepresentation(self.imageOutlet.image!, 0.2)
        let fileToSave = PFFile(name: "myfile.png", data: imageData!)

        let firstObj = objects?.first
        firstObj!["fileToSave"] = fileToSave
        firstObj!.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
            print("Object has been saved.")
        }


    }

同步版本

    let query = PFQuery(className: "FileTest")

    do {
        let objects = try query.findObjects()
        let firstObj = objects.first

        let imageData = UIImageJPEGRepresentation(self.imageOutlet.image!, 0.2)
        let fileToSave = PFFile(name: "myfile.png", data: imageData!)
        firstObj!["fileToSave2"] = fileToSave
        try firstObj!.save()
        print("object saved")


    } catch let er as NSError {
        print("error")
    }