2015-06-17 15 views
10

Tôi thử điều này:Mục tiêu c - ios: Cách chọn video từ Camera Roll?

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

nhưng nó chỉ hiển thị video từ ảnh không phải tất cả video trong thư viện ảnh. Tôi muốn chọn tất cả video từ Camera roll. Hãy giúp tôi bằng cách đưa ra một số đầu mối hoặc mã. Cảm ơn bạn trước!

+0

https://github.com/questbeat/QBImagePicker Có thể được giúp bạn – kb920

+2

Set mediaTypes như 'videoPicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];' và cố gắng để thực thi mã của bạn. – VRAwesome

Trả lời

12

Đâ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]; 
} 
6

Vui lòng sử dụng đoạn mã sau để chọn video từ iOS gallery

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

hãy có một cái nhìn tại liên kết này Select video from gallery

+2

#import để loại bỏ lỗi "loại số nhận dạng không khai báo kuTTypeAVIMovie" từ mã này – Praxiteles

+1

Bạn không ghi đè giá trị cho videoPicker.mediatypes ở đây? (Giá trị đầu tiên của bạn bị bỏ qua.) Bạn có thể xóa định nghĩa đầu tiên của videoPicker.mediaTypes và mã này vẫn hoạt động. – Praxiteles

Các vấn đề liên quan