Aubrey
Aubrey

Reputation: 629

How to save an image to Parse's main User Class?

I have the following method:

- (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]);
        }
    }];
}

I am trying to save a PFObject to the Main User class in Parse. I have column with the profilePicture label but when I run the method, I get a new classed named User (in addition to the existing one) and the image is dropped there. I'm showing an image of my Parse instance with the class and intended object to hold the image.

Any help would be appreciated! :)

Parse DB Image

Upvotes: 0

Views: 4233

Answers (3)

Aubrey
Aubrey

Reputation: 629

Here's what worked: What needed to happen (and @Vidhyanand900 was on the right track, but wasn't 100%) was to save the image in the background and then setObject to set image to the current user.

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...

Answer was found here: https://parse.com/questions/update-table-user-with-a-profile-image


Here's the answer in 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")
}
}

Upvotes: 7

Timothy Walters
Timothy Walters

Reputation: 16874

The User, Role and Installation classes are special, their actual names are _User, _Role and _Installation, but you shouldn't try to use those names either.

When doing queries against those classes you should use the following format:

PFQuery *userQuery = [PFUser query];

Alternatively you can work directly with the current user:

PFUser *currentUser = [PFUser currentUser];

Refer to this section of the documentation on iOS/OS X, Users, Querying.

Upvotes: 1

Vidhyanand
Vidhyanand

Reputation: 5369

You can save image into a Parse using below code..

    PFObject *userInfo = [PFObject objectWithClassName:@"User"];

    NSData *imageData = UIImagePNGRepresentation(selectedImage);

    PFFile *imageFile = [PFFile fileWithName:@"profileimage.png" data:imageData];

    [imageFile save];   

    [userInfo setObject:imageFile forKey:@"profilePicture"];

    [userInfo save];   

Hope it helps you..

Upvotes: 2

Related Questions