从昨天开始,我一直在敲头。我从脸书获取个人资料图片,想保存它进行分析。但我被困住了。我尝试了从这个解决方案
如何在 Swift 中更新 Parse 用户信息?
这并没有解决我的案子。
使用 Swift IOS8 更新 Parse.com 中的用户
我认为情况并非如此,因为我刚刚登录,我的观点也发生了变化。
这是我的代码。
func updateCurrentUserProfilePicture(image: UIImage) {
let currentUser = PFUser.currentUser()
let id = currentUser?.objectId
let data = UIImagePNGRepresentation(image)
var query = PFUser.query()
query!.getObjectInBackgroundWithId(id!) {
(user: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let usr = user {
// usr.setObject(data!, forKey: "image")
usr["image"] = data!
usr.saveInBackgroundWithBlock({ (result: Bool, error: NSError?) in
if error == nil {
self.delegate?.didUpdateProfilePictureWithResult!(true, error: error)
}else {
self.delegate?.didUpdateProfilePictureWithResult!(false, error: error)
}
})
}
}
}
我可以看到didUpdateProfilePictureWithResult
代表被成功召唤。但是当我转到 back4app.com 时,我可以看到user
行,但看不到image
列。而且我也没有看到任何错误。
我在这里错过了什么?
更新我试图在控制台中保存。它保存了,没有任何错误。
**expression do { try usr.save()} catch { print(error)}**
2016-06-10 17:29:32.264 GeofenceMe2[39334:91037] Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
NilError
但在我的 dashboaard 中仍然没有图像列
您无法保存要解析NSData
。在此处查看有效的数据类型:https://parse.com/docs/ios/guide#objects-data-types。
您正在使用相同的用户对象查询用户对象。你可以只使用 PFUser.currentUser()。
func updateCurrentUserProfilePicture(image: UIImage) {
let avatar = PFFile(name: PFUser.currentUser()!.username, data: UIImagePNGRepresentation(image)!)
PFUser.currentUser()!.setObject(avatar!, forKey: "avatar")
PFUser.currentUser()!.saveInBackgroundWithBlock { (success: Bool, error: NSError?) in
}
}
对于 Swift 5.x(基于 @Santhosh 的上述答案):
func updateCurrentUserProfilePicture(profileImage: UIImage) {
guard let currentUser = PFUser.current(), let profileImgPngData = profileImage.pngData() else {
return
}
let avatar = PFFileObject(name: PFUser.current()!.username, data: profileImgPngData)
currentUser.setObject(avatar!, forKey: "profile_image")
currentUser.saveInBackground { success, error in
print(success, error as Any)
}
}