Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 490
Khledon Avatar asked Dec 04 '25 10:12

Khledon


1 Answers

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
    }        
}];
like image 168
Marius Waldal Avatar answered Dec 06 '25 00:12

Marius Waldal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!