我是新手解析并尝试创建一个将用户链接到他/她的照片的PFRelation。这是我的代码:
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];
我知道图片本身正在保存,因为当我让图片保存在后台时,当我点击Pic类时,它会显示我拍摄的图片。但是,我的问题是,当我查看User类时,当我点击myPhotos关系时,没有任何对象处于该关系中。为什么我的图像不会被添加到关系中?
答案 0 :(得分:1)
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]; } }];
这是使用图像保存存储在PFRelation中的方法。我希望这 回答可能对你有所帮助。谢谢。