如何从 UIImagePickerController 中选择任何视频或电影文件

Posted

技术标签:

【中文标题】如何从 UIImagePickerController 中选择任何视频或电影文件【英文标题】:How to select any Video or Movie file from UIImagePickerController 【发布时间】:2012-05-07 10:04:23 【问题描述】:

在我的应用程序中,我需要选择任何视频文件并显示应用程序中视频文件的 URL。

-(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];
    

我已经使用上面的代码显示图像和视频,一切都很好,但是当我选择任何视频文件时,它没有来到 UIImagePickerController 委托方法,视频文件直接播放,我不想播放任何视频文件,当我选择任何视频文件时,它应该出现在其委托属性中

【问题讨论】:

【参考方案1】:

从 iPhone 库中挑选视频不适用于 ios 模拟器。在真正的 iPhone 上试一试。

这是从我在项目中使用的 iOS 照片库中挑选视频的代码。只需将视频方法从选择器添加到您想要的按钮。

- (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];

字符串moviepath 为您提供所选视频的路径,可用于对该视频执行任何所需的操作。

不要忘记将MobileCoreServices.framework 框架添加到您的项目中!然后像这样导入它:

#import <MobileCoreServices/UTCoreTypes.h>

【讨论】:

如果您使用的是 ARC,则可以更轻松地比较媒体类型。 if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeMovie]) 您也可以使用语法糖来创建这样的数组:imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString*) kUTTypeImage];(等等,根据需要添加类型。)相同的功能,只是少了一点输入。 :) 很好的答案:)。我发现通过设置 picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;只打开了一个视频列表,发现它对我所追求的更有用。请记住,不同的源类型会显示不同的菜单(对我来说,使用 UIImagePickerControllerSourceTypePhotoLibrary 会调出我的相册,但随后会显示它们没有视频) 您应该提交一份关于视频在模拟器照片库中不起作用的雷达。未报告的错误不会得到修复;)【参考方案2】:

试试这个:-

   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];

在委托方法中:-

 - (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];

    

【讨论】:

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 Simulator 检查 .h 文件中你有没有忘记写:- 我已经实现了,但是仍然没有任何成功, 如果我这样做,containsObject: 方法是否有效,例如 [((NSString *) kUTTypeVideo) copy] 对 kUTTypeVideo 使用 import MobileCoreServices【参考方案3】:

Swift 3 更新:

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 MobileCoreServices 并添加委托 UIImagePickerControllerDelegateUINavigationControllerDelegate

【讨论】:

【参考方案4】:

@Dinesh Kaushik 的代码帮助我解决了同样的问题。

我变了:

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

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

只有这样,我才能看到视频并选择它。奇怪

【讨论】:

【参考方案5】:

不使用UIImagePicker,只使用assetlibrary,实现自定义视频选择器,直接访问选定的视频文件,保存视频文件并根据需要压缩文件..

【讨论】:

既然 Apple 已经这样做了,为什么还要重新发明***?今天,我们都需要快点,我们中的许多人都一直处于紧张的日程安排中。所以使用现成的UIImagePickerController 绝对是一个好习惯!用户对这个选择器非常熟悉。

以上是关于如何从 UIImagePickerController 中选择任何视频或电影文件的主要内容,如果未能解决你的问题,请参考以下文章

如何调整自定义 UIButton 的图像大小?

iPhone - 以编程方式删除状态栏

imageWithData图像尺寸变大了?

Segue 图像到另一个视图控制器的问题

imagePickerController 选择照片时出错

如何从其他面板从 JTextField 获取输入