Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Reed Sweeney
3,409 PointsRibbit messages containing an image not working correctly
I recently finished the Ribbit self-destruction messaging app and have it loaded on my phone. When I take a video and send it to myself it works correctly. However, when I take a still photo and send it, when I attempt to open it, it opens the movie player and does not load the image. Any tips?
6 Answers
Vikram Pal
Courses Plus Student 101 PointsI know what you're talking about. The problem is in your inbox view controller.
Can you post it so I can see it? I don't have my laptop with me at the moment.
Reed Sweeney
3,409 Points#import "InboxViewController.h"
#import "ImageViewController.h"
#import "MSCellAccessory.h"
@interface InboxViewController ()
@end
@implementation InboxViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
PFUser *currentUser = [PFUser currentUser];
if (currentUser){
NSLog(@"Current user: %@", currentUser.username);
}
else {
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(retrieveMessages) forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:NO];
[self retrieveMessages];
}
#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"];
UIColor *disclosureColor = [UIColor colorWithRed:0.553 green:0.439 blue:0.718 alpha:1.0];
cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_DISCLOSURE_INDICATOR color:disclosureColor];
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;
}
- (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 {
PFFile *videoFile = [self.selectedMessage objectForKey:@"file"];
NSURL *fileUrl = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileUrl;
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
NSMutableArray *recipientIds = [NSMutableArray arrayWithArray:[self.selectedMessage objectForKey:@"recipientIds"]];
NSLog(@"Recipients: %@", recipientIds);
if ([recipientIds count] == 1) {
[self.selectedMessage deleteInBackground];
}
else {
[recipientIds removeObject:[[PFUser currentUser] objectId]];
[self.selectedMessage setObject:recipientIds forKey:@"recipientIds"];
[self.selectedMessage saveInBackground];
}
}
- (IBAction)logout:(id)sender {
[PFUser logOut];
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}
else if ([segue.identifier isEqualToString:@"showImage"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
imageViewController.message = self.selectedMessage;
}
}
#pragma mark - Helper methods
- (void)retrieveMessages {
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];
NSLog(@"Retrieved %d messages", [self.messages count]);
}
if ([self.refreshControl isRefreshing]) {
[self.refreshControl endRefreshing];
}
}];
}
@end
Vikram Pal
Courses Plus Student 101 PointsTry adding my UIImage code after
[recipientIds removeObject:[[PFUser currentUser] objectId]];
[self.selectedMessage setObject:recipientIds forKey:@"recipientIds"];
[self.selectedMessage saveInBackground];
}
}
- (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];
imageGenerator.appliesPreferredTrackTransform = YES;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
}
Reed Sweeney
3,409 PointsMy Xcode is not recognizing AVAsset among other things
Vikram Pal
Courses Plus Student 101 PointsTry importing AVFoundation framework and import it into your header file.
#import <AVFoundation/AVFoundation.h>
Hopefully that makes it work. Sorry missed that little part.
Reed Sweeney
3,409 PointsThat fixed the errors in the code, unfortunately it is still not working on my phone
Vikram Pal
Courses Plus Student 101 PointsOk! I think I got it, after:
self.moviePlayer.contentURL = fileUrl;
[self.moviePlayer prepareToPlay];
Add:
UIImage *thumbnail = [self thumbnailFromVideoAtURL:self.moviePlayer.contentURL];
UIImageView *imageView = [[UIImageView alloc] initWithImage:thumbnail];
[self.moviePlayer.backgroundView addSubview:imageView];
Reed Sweeney
3,409 PointsStill only working if I take a video! For some reason when I take a photo it still tries to load it as a video but nothing shows up (just keeps saying "loading").
Vikram Pal
Courses Plus Student 101 PointsHmm maybe Amit Bijlani can pitch in since I can't seem to find the error then.