xcode Objective c - ios:如何从相机胶卷中选择视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30882321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:19:28  来源:igfitidea点击:

Objective c - ios : How to pick video from Camera Roll?

iosobjective-ciphonexcodeuiimagepickercontroller

提问by user3566863

I try this :

我试试这个:

 UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
videoPicker.delegate = self;
videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
videoPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];
videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self presentViewController:videoPicker animated:YES completion:nil];

but it only show videos from photos part not all video in camera roll. I want to pick all video from Camera roll. Please help me by giving some clue or code. Thanks in advance!

但它只显示照片中的视频,而不是相机胶卷中的所有视频。我想从相机胶卷中选择所有视频。请提供一些线索或代码来帮助我。提前致谢!

回答by Praxiteles

Here is a compilation and cleanup of the answer above plus a few more details.

这是上述答案的汇编和清理以及更多详细信息。

(0) Ensure you have these two includes.

(0) 确保你有这两个包含。

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h> // needed for video types

(1) Add two delegates to your .h file: UINavigationControllerDelegate and UIImagePickerControllerDelegate. For us, it looked like this:

(1) 在.h 文件中添加两个委托:UINavigationControllerDelegate 和 UIImagePickerControllerDelegate。对我们来说,它看起来像这样:

@interface ATHViewController () <UITabBarDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate>

(2) Present the user with choice of videos from their library. Add this code to your .m file somewhere.

(2) 向用户展示从他们的库中选择的视频。将此代码添加到您的 .m 文件中的某处。

    // Present videos from which to choose
    UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
    videoPicker.delegate = self; // ensure you set the delegate so when a video is chosen the right method can be called

    videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
    // This code ensures only videos are shown to the end user
    videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];

    videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    [self presentViewController:videoPicker animated:YES completion:nil];

(3) Handle the responses from the picker. Add this code to your delegate class.

(3) 处理来自picker的响应。将此代码添加到您的委托类。

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

    // This is the NSURL of the video object
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    NSLog(@"VideoURL = %@", videoURL);
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

(4) Handle when the user cancels choosing anything.

(4) 用户取消选择时的处理。

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

回答by Vinodh

Please use the following code to select video from iOS gallery

请使用以下代码从iOS图库中选择视频

UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
videoPicker.delegate = self;
videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
videoPicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];??
videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];
videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self presentViewController:videoPicker animated:YES completion:nil];

Please have a look at this link Select video from gallery

请查看此链接从图库中选择视频