Question

In my app I'm allowing the user to select if they want to choose a photo from the library, use the camera to take a photo, or use the camera to record a video.

After a photo has been selected or taken with the Camera, tapping Use will dismiss the UIImagePickerController and take the user to a new UIViewController.

I want to do the same for when a user records a video. Right now if the user taps the 'Use' button it just dismisses the picker.

How do I detect if the picker is set to record video?

Here's what I have tried:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
 {

     if (picker.mediaTypes == [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera] && picker.cameraCaptureMode == UIImagePickerControllerCameraCaptureModeVideo)
     {
          // Push to new view controller
          // dismiss the picker
     }

 }

Is it possible to do this? What should I check for?

Thanks!

Was it helpful?

Solution

Refer to Apple's "Camera Programming Topics for iOS". The recommendation there is to implement imagePickerController:didFinishPickingMediaWithInfo: instead of imagePickerController:didFinishPickingImage:editingInfo: (which has been deprecated since iOS 3). Then, one can inspect the value of the info dictionary's UIImagePickerControllerMediaType key and compare against the appropriate types.

For example (note that you'll need to add the MobileCoreServices framework, and import its header):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *pickedMediaType = info[UIImagePickerControllerMediaType];
    NSString *movieType = (__bridge NSString *)kUTTypeMovie;
    NSString *imageType = (__bridge NSString *)kUTTypeImage;

    if ([pickedMediaType isEqualToString:movieType]) {
        NSLog(@"movie was picked");
    }
    else if ([pickedMediaType isEqualToString:imageType]) {
        NSLog(@"image was picked");
    }
    else {
        NSLog(@"something else was picked");
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

In your application, you'd replace the logging statements with the appropriate actions (From your question, it sounds like you already have code to push to the new view controllers, so replace the logging statements accordingly). Also, note that you generally shouldn't compare Objective-C objects with ==. As I'm comparing strings, I use isEqualToString:.

OTHER TIPS

use the below stated code that detects the recording of video an at the end of dismissal it goes to detailviewcontroller

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{   
[picker dismissModalViewControllerAnimated:YES];

NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];  

//not production code,  do not use hard coded string in real app
if ( [mediaType isEqualToString:@"public.image" ]) 
{                                                                                                        
    NSLog(@"Picked a photo");
}
//not production code,  do not use hard coded string in real app
else if ( [ mediaType isEqualToString:@"public.movie" ])
{
    NSLog(@"Picked a movie at URL %@",  [info objectForKey:UIImagePickerControllerMediaURL]);
    NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];
    NSLog(@">>>>>>>>>>> Url %@", [url absoluteString]);
    RecordDetailiPhone *objDetail = [[RecordDetailiPhone alloc] initWithNibName:@"RecordDetailiPhone" bundle:nil];
    [objDetail saveVideoFileToDeractory:url];
    [self.navigationController pushViewController:objDetail animated:YES];
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top