Cover by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

11.3. Taking Videos with the Camera

Problem

You want to allow your users to shoot a video using their 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)viewDidLoad{
  [super viewDidLoad];
  
  if ([self isCameraAvailable] &&
      [self doesCameraSupportTakingPhotos]){
    
    UIImagePickerController *controller = 
      [[UIImagePickerController alloc] init];
    
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    NSString *requiredMediaType = (__bridge NSString *)kUTTypeMovie;
    controller.mediaTypes = [[NSArray alloc] 
                             initWithObjects:requiredMediaType, nil];
     controller.allowsEditing = YES;
    controller.delegate = self;
    
    [self.navigationController presentModalViewController:controller
                                                 animated:YES];
    
  } else {
    NSLog(@"Camera is not available.");
  }
  
}

Note

The isCameraAvailable and doesCameraSupportShootingVideos methods used in this sample code are implemented and discussed in Recipe 11.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 objectForKey: UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeMovie]){ NSURL *urlOfVideo = [info objectForKey:UIImagePickerControllerMediaURL]; ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required