Khledon
Khledon

Reputation: 183

Parse.com saving image IOS

I'm trying to save an image to parse.com with the following code:

        NSData *imageData = UIImagePNGRepresentation(profileImage);
        PFFile *imageFile = [PFFile fileWithName:@"Profileimage.png" data:imageData];
        [imageFile saveInBackground];


        PFUser *user = [PFUser currentUser];
        user[@"fullName"] = name;
        user[@"Location"] = location;
        user[@"gender"] = gender;
        user[@"email"] = email;
        user[@"ProfilePic"] = imageFile;
        [user saveInBackground];

The problem is that this doesn't seem to be saving the image file to parse as nothing is populated in my data browser. The code here looks fine to me, but can you guys see anything wrong with it?

The image is being downloaded from Facebook with the following code:

NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];
            NSData *data = [[NSData alloc] initWithContentsOfURL:pictureURL];

            UIImage *profileImage = [[UIImage alloc] initWithData:data];

Any ideas?

Thanks

Upvotes: 3

Views: 4907

Answers (1)

Marius Waldal
Marius Waldal

Reputation: 9932

The problem is that the [imageFile saveInBackground]; operation has not yet been performed when you call [user saveInBackground];

When you call the first background save, the program just continues on.

Use saveInBackgroundWithBlock instead, and then do the [user saveInBackground] operation there.

NSData *imageData = UIImagePNGRepresentation(profileImage);
PFFile *imageFile = [PFFile fileWithName:@"Profileimage.png" data:imageData];
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {
        if (succeeded) {
           PFUser *user = [PFUser currentUser];
            user[@"fullName"] = name;
            user[@"Location"] = location;
            user[@"gender"] = gender;
            user[@"email"] = email;
            user[@"ProfilePic"] = imageFile;
            [user saveInBackground];
        }
    } else {
         // Handle error
    }        
}];

Upvotes: 11

Related Questions