How to select any Video or Movie file from UIImagePickerController
Asked Answered
B

5

32

In my app, i need to select any video file and show the display the url for a video file in the application.

-(void)viewDidLoad{

    [super viewDidLoad];

    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    self.imgPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage, nil];
    self.imgPicker.allowsEditing = NO;

    [self presentModalViewController:self.imgPicker animated:YES];
}

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

    [picker dismissModalViewControllerAnimated:YES];
}


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

}

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

    value = 1;

    IMAGE_COUNTER = IMAGE_COUNTER + 1;

    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ( [mediaType isEqualToString:@"public.movie" ])
    {
        NSLog(@"Picked a movie at URL %@",  [info objectForKey:UIImagePickerControllerMediaURL]);
        NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"> %@", [url absoluteString]); 
    } 

    else
    {
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        NSData* imageData = UIImageJPEGRepresentation(image, 1.0);

        NSString* incrementedImgStr = [NSString stringWithFormat: @"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER];

        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];

        NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
        [imageData writeToFile:fullPathToFile2 atomically:NO];
    }
}

I have used the above code to display image and video , every thing is coming fine, however when i select any video file, it didn't comes to UIImagePickerController Delegate Method, Video file directly plays, i don't want to play any video file, when i select any video file it should come in its delegate property

Brut answered 7/5, 2012 at 10:4 Comment(0)
A
67

Picking videos from the iPhone Library does not work on the iOS Simulator. Try it on a real iPhone.

Here is the code for picking video from the iOS Photo Library which I have used in my projects. Just add video method from selector to your desired button.

- (void)video {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

    [self presentModalViewController:imagePicker animated:YES];
 }


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

    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
        NSString *moviePath = [videoUrl path];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
        }
    }

    [self dismissModalViewControllerAnimated:YES];
    [picker release];
}

The string moviepath gives you the path of the selected video which can be used to perform any desired action with that video.

Don't forget to add the MobileCoreServices.framework Framework to your project! Then import it like this:

#import <MobileCoreServices/UTCoreTypes.h>
Adanadana answered 10/5, 2012 at 10:5 Comment(4)
If you're using ARC, you can compare the media type a little more easily. if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeMovie]) {Jahdal
You can also use syntactic sugar to create the array like this: imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString*) kUTTypeImage]; (etc., add types as desired.) Same functionality, just a bit less typing. :)Presbyterial
Great answer :). I found that by setting picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; brought up only a list of videos and found it was more useful for what I was after. Bear in mind the different source types will show different menus (for me using UIImagePickerControllerSourceTypePhotoLibrary brought up my photo albums but then displayed them empty of video)Scrip
You should file a radar about videos not working in the photo library in the simulator. Unreported bugs don't get fixed ;)Ange
R
7

try this:-

   UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
    NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
    if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
    {
        //NSLog(@"no video");
    }


    [self presentModalViewController:imagePicker animated:YES];

In delegate method:-

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
            NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
            //NSLog(@"type=%@",type);
            if ([type isEqualToString:(NSString *)kUTTypeVideo] || 
                [type isEqualToString:(NSString *)kUTTypeMovie])
            {// movie != video
                NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
}
    }
Rectangular answered 7/5, 2012 at 10:20 Comment(5)
Thanks Gypsa for quick reply however its not working, its still not coming under delegate method, when select any movie, I have 3 m4v movies in SimulatorBrut
check that in .h file you have not forgot to write:-<UIImagePickerControllerDelegate,UINavigationControllerDelegate>Rectangular
I have implemented that however not any success still,Brut
Does the containsObject: method work if I do, for example, [((NSString *) kUTTypeVideo) copy]?Splanchnology
use import MobileCoreServices for kUTTypeVideoCircumflex
G
3

Swift 3 update :

func galleryVideo() 
{        
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {

        let videoPicker = UIImagePickerController()
        videoPicker.delegate = self
        videoPicker.sourceType = .photoLibrary
        videoPicker.mediaTypes = [kUTTypeMovie as String]
        self.present(videoPicker, animated: true, completion: nil)
    }
}

Import import MobileCoreServices and adding delegates UIImagePickerControllerDelegate and UINavigationControllerDelegate .

Groos answered 28/8, 2017 at 10:36 Comment(0)
E
1

@Dinesh Kaushik's code help to with the same problem I had.

I changed :

 //NSArray * availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        //imagePickerController.mediaTypes = availableMediaTypes;

        imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

and only this way, I can see the video and select it. strange

Electrochemistry answered 15/11, 2013 at 17:58 Comment(0)
G
-5

Do not use UIImagePicker, just use assetlibrary, implement custom video picker, access to selected video file directly, save the video file and compess the file if you want..

Grigsby answered 26/3, 2015 at 15:10 Comment(1)
Why reinventing the wheel when Apple did it already? Today we all need to hurry and many of us are permanently on a tight schedule. So using the ready-implemented UIImagePickerController is definitely good practice! As a great plus the users are well familiar with this picker.Fantasia

© 2022 - 2024 — McMap. All rights reserved.