2015-04-01 34 views
0

我是新來分析,並試圖創建一個鏈接用戶到他/她的圖片的PFRelation。這裏是我的代碼:iPhone - 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關係時,沒有對象處於該關係中。爲什麼我的圖像不會添加到關係中?

回答

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存儲方式。我希望這個 的答案可能對你有幫助。謝謝。