I am new to parse and am trying to create a PFRelation that links a user to his/her pictures. Here is my code:
NSData *imageData = UIImagePNGRepresentation(myImage);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
PFObject *pic = [PFObject objectWithClassName:@"Pics"];
//[pic setObject:imageFile forKey:@"Image"];
pic[@"Image"] = imageFile;
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"myPhotos"];
[relation addObject:pic];
[user saveInBackground];
//[pic saveInBackground];
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainScreen"];
[self presentViewController:myViewController animated:YES completion:nil];
I know the picture itself is saving, because when I let the picture save in the background, when I click on the Pic class it shows me the picture I took. But, my problem is that when I am looking at the User class and when I click on the myPhotos relation, no objects are in that relation. Why won't my images be added to the relation?
PFObject *details = [PFObject objectWithClassName:@"**your table name**"]; NSData *imageData = UIImagePNGRepresentation(myImage); PFObject *picDetails = [PFObject objectWithClassName:@"**myphotos**”]; picDetails [@"picture"] = [UIImage imagewithdata:imageData]; [Details save]; [picDetails save]; FRelation *relation = [Details relationForKey:@"**myphotos**"]; [relation addObject:details]; [Details saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { //Do something after the last save... if (succeeded) { // The object has been saved. UIAlertView *success_alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Successfully image saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [success_alert show]; } else { // There was a problem, check error.description UIAlertView *fail_alert = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to save data" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [fail_alert show]; } }];
This is the way to store in PFRelation using image save. I hope this answer may help to you. Thank you.