如何从UIImagePickerController中选择任何视频或电影文件 [英] How to select any Video or Movie file from UIImagePickerController

查看:94
本文介绍了如何从UIImagePickerController中选择任何视频或电影文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app, i need to select any video file and show the display the url for a video file in the application.

-(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委托方法,视频文件直接播放,我不想播放任何视频文件,当我选择任何视频文件时应该来在其委托属性中

I have used the above code to display image and video , every thing is coming fine, however when i select any video file, it didn't comes to UIImagePickerController Delegate Method, Video file directly plays, i don't want to play any video file, when i select any video file it should come in its delegate property

推荐答案

从iPhone库中挑选视频无法在iOS模拟器上运行。在真正的iPhone上试试吧。

Picking videos from the iPhone Library does not work on the iOS Simulator. Try it on a real iPhone.

以下是从我在项目中使用的iOS Photo Library中挑选视频的代码。只需将选择器中的视频方法添加到所需的按钮。

Here is the code for picking video from the iOS Photo Library which I have used in my projects. Just add video method from selector to your desired button.

- (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 为您提供所选视频的路径,可用于对该视频执行任何所需操作。

The string moviepath gives you the path of the selected video which can be used to perform any desired action with that video.

不要忘记添加 MobileCoreServices .framework 项目框架!然后像这样导入:

Don't forget to add the MobileCoreServices.framework Framework to your project! Then import it like this:

#import <MobileCoreServices/UTCoreTypes.h>

这篇关于如何从UIImagePickerController中选择任何视频或电影文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆