私はバックエンドに Parse.com を使用しています。InboxView (友人から画像を受け取る場所) と ChatView の 2 つのビューがあります。画像を送信するときは、「Messages」クラス(解析)でユーザー(複数のユーザーでもかまいません)のrecipientIdsを含む画像を送信します。InboxView で画像を受信すると、画像を送信した友人の名前が行のセルに表示されます。すべての受信者 ID ではなく、画像を送信したユーザーの PFUser objectId を格納する行 (画像を送信したユーザー名) を選択したときに、それを希望します。どうすればそれを作ることができますか?
これが私のコードです:
受信箱.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
および 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