How to record video files in your iOS App

Recording video is very similar to taking a picture with the built-in device camera: we can use the UIImagePickerController – all we do is to specify the video camera for picking actions.

Here’s how can create it:

#import <MobileCoreServices/UTCoreTypes.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (IBAction)recordButton:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        picker.allowsEditing = NO;
        NSArray *mediaTypes = [[NSArray alloc]initWithObjects:(NSString *)kUTTypeMovie, nil];
        picker.mediaTypes = mediaTypes;
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"I'm afraid there's no camera on this device!" delegate:nil cancelButtonTitle:@"Dang!" otherButtonTitles:nil, nil];
        [alertView show];

Note the very first line for importing the Mobile Core Services framework – it needs to be imported and linked to in your project. This is not mentioned anywhere in the official Apple Documentation at the time of writing! We need this to specify the media types array for the image picker. With it we’re saying “only allow movies”. Without the framework Xcode won’t know what kUTTypeMovie is.

If the device doesn’t have a camera this method would crash (Simulator for example, or iPhone 3G), hence the if/then statement.

When the picker is finished you can access the movie URLs in its delegate method which we need to conform, just as we do with picking pictures:

#pragma mark - Delegate Methods

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    // user hit cancel
    [self dismissViewControllerAnimated:YES completion:nil];

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // grab our movie URL
    NSURL *chosenMovie = [info objectForKey:UIImagePickerControllerMediaURL];
    // save it to the documents directory (option 1)
    NSURL *fileURL = [self grabFileURL:@""];
    NSData *movieData = [NSData dataWithContentsOfURL:chosenMovie];
    [movieData writeToURL:fileURL atomically:YES];
    // save it to the Camera Roll (option 2)
    UISaveVideoAtPathToSavedPhotosAlbum([chosenMovie path], nil, nil, nil);
    // and dismiss the picker
    [self dismissViewControllerAnimated:YES completion:nil];

Here we create a new URL in our Documents directory (via the grabFileURL method), turn movie URL into data, and use an NSData method to save our video data into our own URL. In addition and for demonstration we’re also saving our video to the Camera Roll.

The last line dismisses the picker. We do the same when the user hits cancel.

There’s a quick and dirty Demo Project on GitHub to illustrate all this.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.