32

在我的应用程序中,我需要选择任何视频文件并显示应用程序中视频文件的 url。

-(void)viewDidLoad{

    [super viewDidLoad];

    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    self.imgPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage, nil];
    self.imgPicker.allowsEditing = NO;

    [self presentModalViewController:self.imgPicker animated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissModalViewControllerAnimated:YES];
}


-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo{

}

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

    value = 1;

    IMAGE_COUNTER = IMAGE_COUNTER + 1;

    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ( [mediaType isEqualToString:@"public.movie" ])
    {
        NSLog(@"Picked a movie at URL %@",  [info objectForKey:UIImagePickerControllerMediaURL]);
        NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"> %@", [url absoluteString]); 
    } 

    else
    {
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        NSData* imageData = UIImageJPEGRepresentation(image, 1.0);

        NSString* incrementedImgStr = [NSString stringWithFormat: @"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER];

        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];

        NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
        [imageData writeToFile:fullPathToFile2 atomically:NO];
    }
}

我已经使用上面的代码显示图像和视频,一切都很好,但是当我选择任何视频文件时,它没有来到 UIImagePickerController 委托方法,视频文件直接播放,我不想播放任何视频文件,当我选择任何视频文件时,它应该出现在其委托属性中

4

5 回答 5

67

从 iPhone 库中挑选视频不适用于 iOS 模拟器。在真正的 iPhone 上试用。

这是从我在项目中使用的 iOS 照片库中挑选视频的代码。只需将视频方法从选择器添加到您想要的按钮。

- (void)video {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

    [self presentModalViewController:imagePicker animated:YES];
 }


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
        NSString *moviePath = [videoUrl path];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
        }
    }

    [self dismissModalViewControllerAnimated:YES];
    [picker release];
}

该字符串 moviepath 为您提供所选视频的路径,可用于对该视频执行任何所需的操作。

不要忘记将MobileCoreServices.framework框架添加到您的项目中!然后像这样导入它:

#import <MobileCoreServices/UTCoreTypes.h>
于 2012-05-10T10:05:24.737 回答
7

试试这个:-

   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];
}
    }
于 2012-05-07T10:20:49.043 回答
2

斯威夫特 3 更新:

func galleryVideo() 
{        
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {

        let videoPicker = UIImagePickerController()
        videoPicker.delegate = self
        videoPicker.sourceType = .photoLibrary
        videoPicker.mediaTypes = [kUTTypeMovie as String]
        self.present(videoPicker, animated: true, completion: nil)
    }
}

导入导入 MobileCoreServices并添加委托UIImagePickerControllerDelegateUINavigationControllerDelegate

于 2017-08-28T10:36:27.717 回答
1

@Dinesh Kaushik 的代码有助于解决我遇到的同样问题。

我变了 :

 //NSArray * availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        //imagePickerController.mediaTypes = availableMediaTypes;

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

只有这样,我才能看到视频并选择它。奇怪的

于 2013-11-15T17:58:59.530 回答
-4

不要使用 UIImagePicker,只需使用资产库,实现自定义视频选择器,直接访问选定的视频文件,保存视频文件并根据需要压缩文件..

于 2015-03-26T15:10:55.557 回答