我正在尝试使用Parse作为PFFile保存配置文件图像。基本上,当前用户可以选择图像,图像应该保存在profilePic下的Profile类中,以便我以后检索。代码运行时没有错误,但图像没有在Parse中更新。它仍然具有我在LogIn为该用户设置的原始配置文件占位符图像,而不是所选的新图像。我是Swift和Parse的新手。如果您还有其他需要回答的问题,请告诉我。这是我在imagePickerController中的代码:
self.dismissViewControllerAnimated(true, completion: nil)
ProfileImage.image = image
let profilePicData = UIImagePNGRepresentation(ProfileImage.image)
let profilePicFile = PFFile(name: "imageProfile.png", data: profilePicData)
var query = PFQuery(className: "Profile")
query.findObjectsInBackgroundWithBlock({ ( objects, error) -> Void in
if let objects = objects {
for object in objects{
var username = object["username"] as! String
var id = object["userId"] as! String
var profilePic = object["profilePic"] as! PFFile
if id == PFUser.currentUser()?.objectId{
profilePic = profilePicFile
object.saveInBackgroundWithBlock{(success, error) -> Void in
if(error == nil)
{
println("success")
}
}
}
}
}
})
答案 0 :(得分:0)
Swift不使用变量profilePic的引用,所以当你更新它时你没有更新你的对象,你需要更新你用来保存的对象变量,如下所示:
var profilePic = object["profilePic"] as! PFFile
if id == PFUser.currentUser()?.objectId{
// profilePic = profilePicFile - change this
object["profilePic"] = profilePicFile // for this
object.saveInBackgroundWithBlock{(success, error) -> Void in
if(error == nil)
{
println("success")
}
}
}
答案 1 :(得分:0)
https://www.parse.com/docs/ios/guide#files
最后,在保存完成后,您可以像其他任何数据一样将PFFile关联到PFObject:
在将PFFile分配给对象之前,必须保存它。实际上,正如Icaro指出的那样,你必须将图片直接分配给对象
ProfileImage.image = image
let profilePicData = UIImagePNGRepresentation(ProfileImage.image)
let profilePicFile = PFFile(name: "imageProfile.png", data: profilePicData)
var query = PFQuery(className: "Profile")
query.findObjectsInBackgroundWithBlock({ ( objects, error) -> Void in
if(error == nil) {
let profiles = (objects as? [PFObject]) ?? []
for profile in profiles{
var username = profile["username"] as! String
var id = profile["userId"] as! String
var profilePic = profile["profilePic"] as! PFFile
if id == PFUser.currentUser()?.objectId{
profilePicFile.save() //I would recommend you to do a save in background
profile["profilePic"] = profilePicFile
profile.saveInBackgroundWithBlock{(success, error) -> Void in
if(error == nil)
{
println("success")
}
}
}
}
}
})