How to store PFUser objectId with RecipientId

浪尽此生 提交于 2019-12-12 01:50:14

问题


I use Parse.com for backend, I've two views : InboxView (where I receive images from friends), and ChatView. When I send images, I send images with recipientIds of user (that can be multiple users) in "Messages" class (parse). I receive images in my InboxView, I can see in cell for row the name of the friend that send images. I would like that when I select a row (username who sends me image) that stores the PFUser objectId of the user who sends me the image, and not all of the recipientIds. How can I make that ?

Here is my code :

Inbox.h :

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <MediaPlayer/MediaPlayer.h>

@interface InboxViewController : UITableViewController

@property (nonatomic, strong) NSArray *messages;
@property (nonatomic, strong) PFObject *selectedMessage;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@property (nonatomic, strong) UIImage *MindleNav;

- (UIImage *)resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height;



@end

and inbox.m :

#import "InboxViewController.h"
#import "ImageViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LoginViewController.h"
#import "FriendsViewController.h"

@interface InboxViewController ()

@end

@implementation InboxViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.MindleNav = [UIImage imageNamed:@"MindleNavItem.png"];
    UIImage *newImage = [self resizeImage:self.MindleNav toWidth:150.0f andHeight:48.0f];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:newImage];
    self.MindleNav = newImage;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {
            // We found messages!
            self.messages = objects;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.messages count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject *message = [self.messages objectAtIndex:indexPath.row];
    cell.textLabel.text = [message objectForKey:@"senderName"];

    NSString *fileType = [message objectForKey:@"fileType"];
    if ([fileType isEqualToString:@"image"]) {
        cell.imageView.image = [UIImage imageNamed:@"icon_image"];
    }
    else {
   //     cell.imageView.image = [UIImage imageNamed:@"icon_video"];
    }

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedMessage = [self.messages objectAtIndex:indexPath.row];
    NSString *fileType = [self.selectedMessage objectForKey:@"fileType"];
    if ([fileType isEqualToString:@"image"]) {
        [self performSegueWithIdentifier:@"showImage" sender:self];
    }
    else {
        // File type is text




//        // File type is video
//        PFFile *videoFile = [self.selectedMessage objectForKey:@"file"];
//        NSURL *fileUrl = [NSURL URLWithString:videoFile.url];
//        self.moviePlayer.contentURL = fileUrl;
//        [self.moviePlayer prepareToPlay];
//        UIImage *thumbnail = [self thumbnailFromVideoAtURL:self.moviePlayer.contentURL];
//
////        [self.moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//        
//        // Add it to the view controller so we can see it
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:thumbnail];
//        [self.moviePlayer.backgroundView addSubview:imageView];
//        [self.moviePlayer setFullscreen:YES animated:YES];
    }

    // Delete it!
    NSMutableArray *recipientIds = [NSMutableArray arrayWithArray:[self.selectedMessage objectForKey:@"recipientIds"]];
    NSLog(@"Recipients: %@", recipientIds);

    if ([recipientIds count] == 1) {
        // Last recipient - delete!
      //  [self.selectedMessage deleteInBackground];
    }
    else {
        // Remove the recipient and save
//        [recipientIds removeObject:[[PFUser currentUser] objectId]];
//        [self.selectedMessage setObject:recipientIds forKey:@"recipientIds"];
//        [self.selectedMessage saveInBackground];
    }

}




- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showImage"]) {
        [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
        ImageViewController *imageViewController =
        (ImageViewController *)segue.destinationViewController;
        imageViewController.message = self.selectedMessage;
    }
}

- (UIImage *)thumbnailFromVideoAtURL:(NSURL *)url
{
    AVAsset *asset = [AVAsset assetWithURL:url];

    //  Get thumbnail at the very start of the video
    CMTime thumbnailTime = [asset duration];
    thumbnailTime.value = 0;

    //  Get image from the video at the given time
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return thumbnail;
}

- (UIImage *)resizeImage:(UIImage *)truckImage toWidth:(float)width andHeight:(float)height {
    CGSize newSize = CGSizeMake(width, height);
    CGRect newRectangle = CGRectMake(0, 0, width, height);
    UIGraphicsBeginImageContext(newSize);
    [self.MindleNav drawInRect:newRectangle];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resizedImage;
}

@end

来源:https://stackoverflow.com/questions/27297817/how-to-store-pfuser-objectid-with-recipientid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!