0

我是解析新手,正在尝试创建一个将用户链接到他/她的图片的 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 关系时,该关系中没有对象。为什么我的图像不会添加到关系中?

4

1 回答 1

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 中存储的方式。我希望这个答案对你有所帮助。谢谢你。

于 2015-04-03T06:31:14.277 回答