在 swift 中将 JPEG 上传到 Parse 时出现问题

Problems uploading JPEG to Parse in swift

我在成功保存 JPEG 图像后无法显示在 Parse 上。这是我的代码:

var createPost = PFObject(className: "Loading_Post")

createPost["userObjectId"] = PFUser.currentUser()?.objectId

let imageOne = image1.image as UIImage!
let imageData1 = UIImageJPEGRepresentation(imageOne, 0.5)
let imageFile1 = PFFile(name: "image1.jpg", data: imageData1!)

print(imageFile1)

createPost["image1"] = imageFile1

createPost.saveInBackgroundWithBlock { (success, error) -> Void in
    if error == nil {
        self.displayAlert("success! ", message: "Your Post has been saved successfully")
    } else {
        self.displayAlert("Could not post image", message: "Please try again later")
    }
}

代码运行并且所有内容都正确存储在 Parse 中,除了图像仍然 "undefined",但我的打印语句 returns:

<PFFile: 0x7fed357c8580>

谢谢!

可能您需要先保存图像的PFFile,然后再保存到PFObject。试试这个,

imageFile1.saveInBackgroundWithBlock { (success, error) -> Void in

  if error == nil {

     var createPost = PFObject(className: "Loading_Post")

     createPost["userObjectId"] = PFUser.currentUser()?.objectId

     createPost["image1"] = imageFile1

     createPost.saveInBackgroundWithBlock { (success, error) -> Void in

        if error == nil {

            self.displayAlert("success! ", message: "Your Post has been saved successfully")

        } else {

            self.displayAlert("Could not post image", message: "Please try again later")
        }

  } else {

     self.displayAlert("Could not post image", message: "Please try again later")
  }

尝试保存以进行解析时,这是一个简单的拼写错误。 "image1" 应该是 "Image1"。抱歉没有早点发现!