压缩视频

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    
    //创建图片选择控制器
    
    UIImagePickerController * pick = [[UIImagePickerController alloc]init];
    
    
    //设置数据类型
    
    pick .sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    //设置媒体类型
    
    pick.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    
    //设置代理
    pick.delegate = self;
    
    
    //moda 控制器
    
    [self presentViewController:pick animated:YES completion:nil];
    
    
    
    
}





- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;{
    
//    NSLog(@"%@" , info);
    NSURL * url = info[UIImagePickerControllerMediaURL];
    
    //包装
    AVAsset * set = [AVAsset assetWithURL:url];
    
    //创建资源导出会话
    
    /*
     NSString *const AVAssetExportPresetLowQuality;
     NSString *const AVAssetExportPresetMediumQuality;
     NSString *const AVAssetExportPresetHighestQuality;
     
     */
    
    AVAssetExportSession * exportSession = [AVAssetExportSession exportSessionWithAsset:set presetName:AVAssetExportPresetHighestQuality];
    
    //设置视屏存储路径
    NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"压缩视屏.MP4"];
    
    //导出路径
    exportSession.outputURL = [NSURL fileURLWithPath:path];
    
    NSLog(@"%@" , exportSession.outputFileType);
    
    //一定要设置导出视频类型
    exportSession.outputFileType = @"public.mpeg-4";
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
       
        NSLog(@"finshed");
        
        
    }];
    
}

  

原文地址:https://www.cnblogs.com/yuwei0911/p/5448984.html