0

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
Vjardel
  • 1,065
  • 1
  • 13
  • 28
  • some code would be nice to get a feel for your vision – soulshined Dec 04 '14 at 17:12
  • Great! Thanks, you'll have to forgive me, i'm on my way to work so I'm trying to do this from an iPhone, hopefully someone will beat me to the answer, but correct me if i'm wrong it's hard to understand your question, you basically want to tap a tableviewcell and do what with only the senderName? – soulshined Dec 04 '14 at 17:27
  • Additionally, are you against using PFQueryTableViewController ? I know you put a lot of effort into coding what you have already but PFQueryTableViewController has a lot of advantages opposed to your slice since your only querying from one class – soulshined Dec 04 '14 at 17:29
  • @soulshined I just want to store 2 PFUser objectId ( current and the sender of the image ). For after seeing the image, the current user can reply to the user directly. Am I understandable ? Thanks for helping – Vjardel Dec 04 '14 at 17:37
  • No i'm sorry, I am having a hard time following :/ maybe a slight language barrier. So you just want to reply to the person that sent you a picture correct? – soulshined Dec 04 '14 at 17:41
  • @soulshined Yes! I click on a picture that I've received. After I've a button "reply", for sending image to the sender – Vjardel Dec 04 '14 at 17:45
  • sorry for the late reply, i've been at work: Have you looked at the Parse Documentation yet? https://parse.com/tutorials/anypic This is a great resource and better than me typing everything out for you. if you still can't figure it out after this let me know – soulshined Dec 04 '14 at 20:36
  • @soulshined Very useful for my kind of app, like add a comment on image, and news feed ! That's what I need for after my problem with "reply button". But I don't see anything on this : User A sends an image to User B. User B sees and opens on inbox the image from user A. User B tap on "reply" button and then he can send another image to user A. – Vjardel Dec 05 '14 at 08:13
  • It's not in that app but that sample code & documentation offers the best resource to learn how to use pointers on the website and code relative to it. It has examples on to retrieve usernames and images and direct you to the person who uploads the image. Your just going to have to learn how to use PFFile and pointers and queries. Their documentation is one of the best. But furthermore your code doesn't have anything said about the view that's segued. How is the persons name displayed in the segued view once didselectrow is initiated? All you have to do is make a query with User class & reply – soulshined Dec 05 '14 at 17:43

0 Answers0