如何将图像保存到Parse的主要用户类?

Aub*_*rey 0 xcode objective-c ios parse-platform

我有以下方法:

- (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]);
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

我试图将PFObject保存到Parse中的主用户类.我有一个带有profilePicture标签的列,但是当我运行该方法时,我得到一个新的分类名为User(除了现有的)并且图像被丢弃.我正在显示我的Parse实例的图像,其中包含用于保存图像的类和预期对象.

任何帮助,将不胜感激!:)

解析数据库图像

Aub*_*rey 7

这里是什么工作:什么需要发生(和@ 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...
Run Code Online (Sandbox Code Playgroud)

答案在这里找到: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")
}
}
Run Code Online (Sandbox Code Playgroud)