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

iOS Build a Self-Destructing Message iPhone App Retrieving and Viewing Data from Parse.com Viewing Videos Using MPMoviePlayerController

my app crashes when i try to save a video to parse

// // cameraTableViewController.m // Ribbit // // Created by PC on 1/26/15. // Copyright (c) 2015 Jenish. All rights reserved. //

import "cameraTableViewController.h"

import <MobileCoreServices/UTCoreTypes.h>

import <Parse/Parse.h>

@interface cameraTableViewController ()

@end

@implementation cameraTableViewController

  • (void)viewDidLoad { [super viewDidLoad]; self.imagepicker=[[UIImagePickerController alloc]init]; self.imagepicker.delegate=self; self.imagepicker.allowsEditing=NO;

    self.recepiants=[[NSMutableArray alloc]init];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    self.imagepicker.sourceType=UIImagePickerControllerSourceTypeCamera; }

    else { self.imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    } self.freindsrelation=[[PFUser currentUser ]objectForKey:@"friendsrelation"];

    self.imagepicker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:self.imagepicker.sourceType]; [self presentViewController:self.imagepicker animated:NO completion:nil];}

-(void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:YES];


PFQuery *query=[self.freindsrelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"erorr"] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alertview show];
    }
    else
    {
        self.friends=objects;
        [self.tableView reloadData];


    }

}];
if (self.namedimage==nil &&[self.Video length]==0) {
    self.imagepicker.videoMaximumDuration=100;

    self.imagepicker=[[UIImagePickerController alloc]init];
    self.imagepicker.delegate=self;
    self.imagepicker.allowsEditing=NO;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {


        self.imagepicker.sourceType=UIImagePickerControllerSourceTypeCamera;
    }
    else
    {
        self.imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    }

    self.imagepicker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:self.imagepicker.sourceType];
    [self presentViewController:self.imagepicker animated:NO completion:nil];
}

}

pragma mark - Table view data source

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section. return [self.friends count]; }

    pragma mark-image picker controller delegat

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [self dismissViewControllerAnimated:NO completion:nil]; [self.tabBarController setSelectedIndex:0];

}

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

    PFUser *user=[self.friends objectAtIndex: indexPath.row]; cell.textLabel.text=user.username; if ([self.recepiants containsObject:user.objectId]) { cell.accessoryType=UITableViewCellAccessoryCheckmark; }

    else { cell.accessoryType=UITableViewCellAccessoryNone;

    }

    return cell; }

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *mediatype=[info objectForKey:UIImagePickerControllerMediaType];

if ([mediatype isEqualToString:(NSString*)kUTTypeImage]) {
    self.namedimage=[info objectForKey:UIImagePickerControllerOriginalImage];
    if (self
        .imagepicker.sourceType==UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(self.namedimage, nil, nil, nil);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
else
{
    //video
    self.Video=CFBridgingRelease([[info objectForKey: UIImagePickerControllerMediaURL]path]);
    if (self
        .imagepicker.sourceType==UIImagePickerControllerSourceTypeCamera) {
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.Video)) {
               UISaveVideoAtPathToSavedPhotosAlbum(self.Video, nil, nil, nil);
        }



    }  [self dismissViewControllerAnimated:YES completion:nil];


}

} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath]; PFUser *user=[self.friends objectAtIndex:indexPath.row]; if (cell.accessoryType==UITableViewCellAccessoryNone) { cell.accessoryType=UITableViewCellAccessoryCheckmark; [self.recepiants addObject:user.objectId]; } else{ cell.accessoryType=UITableViewCellAccessoryNone; [self.recepiants removeObject:user.objectId]; } NSLog(@"%@",self.recepiants);

}

  • (IBAction)Cancel:(id)sender { self.namedimage=nil; self.Video=nil; [self.recepiants removeAllObjects]; [self.tabBarController setSelectedIndex:0];

}

  • (IBAction)Send:(id)sender { if (self.namedimage==nil&&[self.Video length]==0) { UIAlertView *alerview=[[UIAlertView alloc]initWithTitle:@"Try Again!" message:@"Please capture or select a photo or video to share" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] ; [alerview show]; [self presentViewController:self.imagepicker animated:NO completion:nil]; } else { [self uploadmessage];

    [self.tabBarController setSelectedIndex:0];
    

    } } -(void)uploadmessage { NSData *filedata; NSString *filename; NSString *filetype;

    if (self.namedimage!=nil) { UIImage *newimage=[self resizeimage:self.namedimage towidth:320.0f andHeight:480.0f]; filedata=UIImagePNGRepresentation(newimage); filename=@"image.png"; filetype=@"image";

    } else { filedata=[NSData dataWithContentsOfFile:self.Video]; filename=@"video.MOV"; filetype=@"video";

    }

    self.file=[PFFile fileWithName:filename data:filedata]; [self.file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alerview=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"Please try sending again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] ; [alerview show];

    }
    
    else{
        PFObject *message =[PFObject objectWithClassName:@"Messages"];
        [message setObject:self.file forKey:@"file"];
        [message setObject:filetype forKey:@"filytype"];
        [message setObject:self.recepiants forKey:@"recepiants"];
        [message setObject:[[PFUser currentUser]objectId] forKey:@"SenderId"];
        [message setObject:[[PFUser currentUser]username] forKey:@"SenderName"];
        [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alerview=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"Please try sending again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] ;
                [alerview show];}
            else
            {
    
                self.namedimage=nil;
                self.Video=nil;
                [self.recepiants removeAllObjects];
    
            }
        }];
    
    }
    

    }];

} -(UIImage* )resizeimage:(UIImage *)image towidth:(float)width andHeight:(float)height { CGSize newSize=CGSizeMake(width, height); CGRect newrectangle=CGRectMake(0, 0, width, height); UIGraphicsBeginImageContext(newSize);

[image drawInRect:newrectangle];
UIImage *resizedimage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedimage;

} @end

Jack Ryder
Jack Ryder
7,286 Points

Can you paste your code?

You should be creating a PFFile with the data object gathered from the video file path

Then saving it in the background setting the various objects for keys as required and saving these also in the background.

Some of these objects your setting may be nil which could be causing the crash but I can't say much more without the code.

Cheers

1 Answer

Jenish, here is a full template for the social network application: Click Here.