2017-02-27 73 views
0

我試圖上傳一個圖像解析服務器usinf PFFile。我沒有問題從解析儀表板手動上傳並從應用程序中檢索它。但是,如果我嘗試使用此方法上載新圖片,圖片不會更新。Swift 3上傳圖像到解析服務器

場的其餘beeing上傳正確(名字,姓氏,用戶名和電子郵件)

保存方法:

@IBAction func saveBtnPressed(_ sender: Any) { 

    print("Start saving...") 

    let imageData = UIImagePNGRepresentation(profilePic.image!) 
    print ("imageData value:") 
    print(imageData!) 
    let imageFile = PFFile(name:"avatar.png", data:imageData!) 
    print ("imageFile value:") 
    print(imageFile!) 

    let query = PFQuery(className: "_User") 
    query.whereKey("username", equalTo: (PFUser.current()?.username)!) 
    query.findObjectsInBackground(block: { (objects, error) in 
    if let user = objects { 
     for object in user { 
      object["name"] = self.nameFld.text 
      object["lastname"] = self.lastnameFld.text 
      object["username"] = self.emailFld.text 
      object["email"] = self.emailFld.text 
      object["avatar"] = imageFile 

      print(object) 
      object.saveInBackground() 
     } 
    } 
    }) 

} 

輸出

Start saving... 
imageData value: 
8358983 bytes 
(lldb) 
+0

如果你只是想保存'currentUser'圖片,我只看到任何一點查詢用戶,然後更新圖像。您可以簡單地上傳圖像,即「PFFile」,並將其保存爲「currentUser」的頭像。據我所知''imageFile'不能在'findObjectsInBackground'塊中訪問。 – Adeel

回答

3

爲了使這項工作,你需要上傳pi cture只有第一,然後將其引用到用戶

let imageData = UIImagePNGRepresentation(profilePic.image!) 
print ("imageData value:") 
print(imageData!) 
let imageFile = PFFile(name:"avatar.png", data:imageData!) 
print ("imageFile value:") 
print(imageFile!) 

imageFile.saveInBackground { (result, error) in 
    if let error = error{ 
     print(error) 
    }else{ 
     let query = PFQuery(className: "_User") 
     query.whereKey("username", equalTo (PFUser.current()?.username)!) 
     query.findObjectsInBackground(block: { (objects, error) in 
      if let user = objects { 
       for object in user { 
        object["name"] = self.nameFld.text 
        object["lastname"] = self.lastnameFld.text 
        object["username"] = self.emailFld.text 
        object["email"] = self.emailFld.text 
        object["avatar"] = imageFile 

        print(object) 
        object.saveInBackground() 
       } 
      } 
     }) 

    } 
} 
} 

請注意,在上面的例子中的文件始終上傳到服務器上,即使沒有用戶匹配查詢,以便只考慮獲得查詢結果後,上傳文件。

+0

感謝您的回覆。現在它穿這個輸出: 8406442字節 錯誤域= NSCocoaErrorDomain代碼= 3840「JSON文本沒有開始與數組或對象和選項,以允許片段沒有設置。 UserInfo = {NSDebugDescription = JSON文本沒有開始數組或對象和選項,以允許片段沒有設置。} –

+0

@DaniPolo你可以嘗試更改文件初始化爲'let imageFile = PFFile(data:data:imageData !, contentType:「image/png「)' –

+0

它不工作..同樣的問題.. –

1

無需查詢當前用戶。你可以這樣做:

@IBAction func saveBtnPressed(_ sender: AnyObject) { 
    let userToUpdate = PFUser.current()! 

    userToUpdate["name"] = self.nameFld.text 
    userToUpdate["email"] = self.emailFld.text 
    userToUpdate["username"] = self.emailFld.text 

    // Save Avatar 
    if profilePic.image != nil { 
     let imageData = UIImageJPEGRepresentation(profilePic.image!, 0.5) 
     let imageFile = PFFile(name:"avatar.jpg", data:imageData!) 
     userToUpdate["avatar"] = imageFile 
    } 

    // Saving block 
    userToUpdate.saveInBackground(block: { (succ, error) in 
     if error == nil { 
      print("Your Profile has been updated!") 
     } else { 
      print("Failed") 

    }}) 
}