O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.3. Taking Videos with the Camera

Problem

You want to allow your user to shoot a video using his iOS device, and you would like to be able to use that video from inside your application.

Solution

Use UIImagePickerController with the UIImagePickerControllerSourceTypeCamera source type and the kUTTypeMovie media type:

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    static BOOL beenHereBefore = NO;

    if (beenHereBefore){
        /* Only display the picker once as the viewDidAppear: method gets
         called whenever the view of our view controller gets displayed */
        return;
    } else {
        beenHereBefore = YES;
    }

    if ([self isCameraAvailable] &&
        [self doesCameraSupportTakingPhotos]){

        UIImagePickerController *controller =
        [[UIImagePickerController alloc] init];

        controller.sourceType = UIImagePickerControllerSourceTypeCamera;

        controller.mediaTypes = @[(__bridge NSString *)kUTTypeMovie];

        controller.allowsEditing = YES;
        controller.delegate = self;

        [self presentViewController:controller animated:YES completion:nil];

    } else {
        NSLog(@"Camera is not available.");
    }

}

Note

The isCameraAvailable and doesCameraSupportShootingVideos methods used in this sample code are implemented and discussed in Recipe 15.1.

We will implement the delegate methods of the image picker controller like so:

- (void)  imagePickerController:(UIImagePickerController *)picker
  didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSLog(@"Picker returned successfully.");

    NSLog(@"%@", info);

    NSString     *mediaType = info[UIImagePickerControllerMediaType ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required