1

这是我从 imagePicker 中挑选视频的代码:

imagepicker = [[UIImagePickerController alloc] init];
[imagepicker setDelegate:self];      

NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagepicker setMediaTypes:mediaTypesAllowed];

imagepicker.wantsFullScreenLayout = YES;

pickImagePop = [[UIPopoverController alloc] initWithContentViewController:imagepicker];

[pickImagePop setDelegate:self];
[pickImagePop presentPopoverFromRect:CGRectMake(60, 100, 1, 20) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[pickImagePop setPopoverContentSize:CGSizeMake(320, 400) animated:YES];

但是这段代码在 Picker 中同时显示照片和视频。我只想显示视频。我怎样才能做到这一点?

4

3 回答 3

2

如果您只想要视频,您应该添加:

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

请记住将MobileCoreServices框架添加到您的项目中。

编辑:

您应该通过以下方法预先检查设备可以使用哪些类型。

+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType

它将返回一个包含设备支持的所有媒体类型的数组。

于 2012-07-11T07:02:46.673 回答
1

试试这个

 1.Add MobileCoreServices framework to your project and import to your view controller
  #import <MobileCoreServices/UTCoreTypes.h> .
 2.Add this delegate methods in Interface 
    <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
 3.Create a Button Action for Record Video and play video like this

- (IBAction)recordVideo:(UIButton*)sender {
    UIImagePickerController *videoScreen = [[UIImagePickerController alloc] init];
    videoScreen.sourceType = UIImagePickerControllerSourceTypeCamera;

    // Displays movie capture control
    videoScreen.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

    videoScreen.allowsEditing = NO;
    videoScreen.delegate = self;

    [self presentViewController:videoScreen animated: YES completion:NO];
}

//ImagePickerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:NO completion:NO];
    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
        }
    }
}
}

- (IBAction)playVideo:(id)sender {
    UIImagePickerController *mediaLibrary = [[UIImagePickerController alloc] init];
    mediaLibrary.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    mediaLibrary.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

    mediaLibrary.allowsEditing = NO;
    [self presentViewController:mediaLibrary animated:YES completion:NO];
}
于 2016-10-14T13:20:22.853 回答
0
    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];
}
    }
于 2016-09-19T14:09:31.813 回答