我有以下方法:
- (void) savePhotoToUser {
NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 1.0);
PFFile *imageFile = [PFFile fileWithName:@"img.png" data:imageData];
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//THIS IS WHERE I NEED HELP
//
PFObject *userPhoto = [PFObject objectWithClassName:@"User"];
//
//
[userPhoto setObject:imageFile forKey:@"profilePicture"];
[userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Saved %@ for user %@", imageFile, userPhoto);
//Push ViewController
}
else{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
else{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
我正在尝试将 PFObject 保存到 Parse 中的主用户类。我有一个带有 profilePicture 标签的列,但是当我运行该方法时,我得到一个名为 User 的新分类(除了现有的分类之外)并且图像被拖放在那里。我正在显示我的 Parse 实例的图像以及用于保存图像的类和预期对象。
任何帮助将不胜感激! :)
最佳答案
这是有效的: 需要做的(@Vidhyanand900 在正确的轨道上,但不是 100%)是将图像保存在后台,然后 setObject 将图像设置为当前用户。
NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 1.0);
PFFile *imageFile = [PFFile fileWithName:@"img.png" data:imageData];
[imageFile saveInBackground];
PFUser *user = [PFUser currentUser];
[user setObject:imageFile forKey:@"profilePicture"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//...REST OF CODE...
答案在这里找到:https://parse.com/questions/update-table-user-with-a-profile-image
这是 Swift 中的答案:
let imageData: NSData = UIImageJPEGRepresentation(image, 1.0)
let imageFile: PFFile = PFFile(name:"image.jpg", data:imageData)
imageFile.save()
let user = PFUser.currentUser()
user.setObject(imageFile, forKey: "profilePicture")
user.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if (success != nil) {
println("saving")
}
}
关于ios - 如何将图像保存到 Parse 的主要用户类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133598/