Đây là một bộ sưu tập và dọn dẹp các câu trả lời ở trên cộng thêm một vài chi tiết.
(0) Đảm bảo bạn có hai tính năng này.
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h> // needed for video types
(1) Thêm hai đại biểu vào tệp .h của bạn: UINavigationControllerDelegate và UIImagePickerControllerDelegate. Đối với chúng tôi, hình như sau:
@interface ATHViewController() <UITabBarDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate>
(2) Trình bày cho người dùng lựa chọn video từ thư viện của họ. Thêm mã này vào tệp .m của bạn ở đâu đó.
// 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) Xử lý câu trả lời từ bộ chọn. Thêm mã này vào lớp đại biểu của bạn.
- (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) Xử lý khi người dùng hủy bỏ chọn bất kỳ thứ gì.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
https://github.com/questbeat/QBImagePicker Có thể được giúp bạn – kb920
Set mediaTypes như 'videoPicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];' và cố gắng để thực thi mã của bạn. – VRAwesome