带有mp4格式的IOS UIImagePicker [英] IOS UIImagePicker with mp4 format

查看:23
本文介绍了带有mp4格式的IOS UIImagePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以保存视频并将其添加到自定义 ALAsset,以 mp4 格式从 UIImagePicker 捕获?或者我必须将它保存在 .mov 中并通过 AVAssetExportSession 进行压缩?

Is it possible to save video and add it to custom ALAsset, captured from UIImagePicker in mp4 format? Or I have to save it in .mov and make compression by AVAssetExportSession?

推荐答案

是的,您可以使用 AVAssetExportSession 压缩视频.在这里您可以指定压缩视频的视频类型、质量和输出 url.

Yes, you can compress video using AVAssetExportSession. Here you can specify video type, quality and output url for compress video.

参见以下方法:

- (void) saveVideoToLocal:(NSURL *)videoURL {

    @try {
        NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docPath = [documentsDirectory objectAtIndex:0];

        NSString *videoName = [NSString stringWithFormat:@"sampleVideo.mp4"];
        NSString *videoPath = [docPath stringByAppendingPathComponent:videoName];

        NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

        NSLog(@"Loading video");

        [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {

             if (exportSession.status == AVAssetExportSessionStatusCompleted) {
                 NSLog(@"Compression is done");
             }
             [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
         }];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception.description);
        [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
    }
}


//---------------------------------------------------------------

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler {
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        handler(exportSession);
    }];
}

这里我将压缩视频保存到应用程序的文档目录中.您可以在以下示例代码中查看详细工作情况:

Here I saved compress video to document directory of application. You can check detail working of this in below sample code:

这篇关于带有mp4格式的IOS UIImagePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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