简体   繁体   English

Objective c - ios:如何从Camera Roll中选择视频?

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

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! 提前致谢!

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. (1)在你的.h文件中添加两个代理:UINavigationControllerDelegate和UIImagePickerControllerDelegate。 For us, it looked like this: 对我们来说,它看起来像这样:

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

(2) Present the user with choice of videos from their library. (2)向用户呈现其库中的视频选择。 Add this code to your .m file somewhere. 将此代码添加到某处的.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. (3)处理选择器的响应。 Add this code to your delegate class. 将此代码添加到您的委托类。

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

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 请查看此链接从图库中选择视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM