如何将图像保存到解析?

问题描述:

由于某些奇怪的原因,我的图片已停止保存以进行解析。之前,我的代码工作正常,我没有做任何修改。如何将图像保存到解析?

这里是我的代码:

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

      }) 

一切是完美的储存,但什么是我的图像数据的问题? 谢谢!

+0

您不保存parseImageFile,您还需要保存以及 –

+0

如果您要停止学习Swift并使用ObjC,然后在Parse中使用所有教程并安装应用程序,它可能会为您节省大约几个小时的时间这是做到这一点,并在4个小时左右开始工作。 Parse明确表示,他们没有太多兴趣支持Swift,我同意ObjC的7年历史,现在不知何故Swift更容易,但它需要知道Apple的东西,所以不是那么卑鄙,但现实是这个:如果你使用ObjC,那么你的生活将会变得更加容易,因为在那里有多少信息并不难学。 – Loxx

+0

观看这个视频解析,他们解释了他们为什么不全力支持在全油门模式下:http://blog.parse.com/categories/videos/专门观看此视频“问解析任何东西 - 六月版在这里!” – Loxx

在你的代码是不节能parseImageFile,所以首先parseImageFile.SaveInBackground并成功将其设置为posts,然后保存posts以及

应该是这样的

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

       }) 

我没有测试这个代码在编辑器上你可能会发现一些语法错误,但它应该是这样的...

所以事情是当你创建parseImageFile,然后saveInBackground a nd里面块设置它的职位,然后再保存发布

+0

我很理解你在说什么。这说得通。但我无法围绕这种格式进行包装。我应该补充说我是移动开发新手。 – V1P3R

+0

在后台保存帖子的位置? – V1P3R

+0

我已经更新了代码,你的代码应该看起来像这样,我没有在编辑器上测试过,你可能会修正一​​些语法问题... –

我检查了你的代码,它的工作正常。

 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 } 

试试这个,如果这段代码会起作用 - 最简单的方法就是删除“Product”表并重新创建它。

+0

我不断测试我的代码,出于某种原因,使用我的相机拍摄的图像不会保存为解析。任何想法为什么? – V1P3R

+0

我的代码很好,就像你说的。截图张贴没有问题。 – V1P3R

+0

你能告诉我一个字段外观的截图吗?是(未定义)? – vsilux