且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从UIImagePickerController中选择任何视频或电影文件

更新时间:2023-01-28 19:30:06

从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>