如何将图像保存到解析?
问题描述:
由于某些奇怪的原因,我的图片已停止保存以进行解析。之前,我的代码工作正常,我没有做任何修改。如何将图像保存到解析?
这里是我的代码:
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)
}
})
一切是完美的储存,但什么是我的图像数据的问题? 谢谢!
答
在你的代码是不节能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里面块设置它的职位,然后再保存发布
答
我检查了你的代码,它的工作正常。
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”表并重新创建它。
您不保存parseImageFile,您还需要保存以及 –
如果您要停止学习Swift并使用ObjC,然后在Parse中使用所有教程并安装应用程序,它可能会为您节省大约几个小时的时间这是做到这一点,并在4个小时左右开始工作。 Parse明确表示,他们没有太多兴趣支持Swift,我同意ObjC的7年历史,现在不知何故Swift更容易,但它需要知道Apple的东西,所以不是那么卑鄙,但现实是这个:如果你使用ObjC,那么你的生活将会变得更加容易,因为在那里有多少信息并不难学。 – Loxx
观看这个视频解析,他们解释了他们为什么不全力支持在全油门模式下:http://blog.parse.com/categories/videos/专门观看此视频“问解析任何东西 - 六月版在这里!” – Loxx