• iOS 视频选择压缩


     

     

    //原理,还是调用UIImagePickerController控制器,设置Type为视频

     

    #import "ViewController.h"

    #import <AVFoundation/AVFoundation.h>

     

    @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        //打开控制器

        UIImagePickerController *picker=[[UIImagePickerController alloc]init];

        //picker.sourceType=UIImagePickerControllerSourceTypeCamera;//设置摄像头类型 摄像头

        //picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//打开相册,获取资源,视频和图片用户拍摄的照片可以删除,

        //picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//打开相册库,用户与电脑同步的,不能删除

        picker.delegate=self;

        //设置媒体类型

        picker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

        //显示控制器

        [self presentViewController:picker animated:YES completion:nil];

        

        

    }

    #pragma mark UIImagePickerControllerDelegate

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

    {

        //如果是视频返回的是URL

        NSURL*url=info[UIImagePickerControllerMediaURL];

        //使用媒体工具 压缩

        [self compressVideo:url];

        //关闭控制器

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    //压缩

    -(void)compressVideo:(NSURL*)url

    {

        //使用媒体工具(AVFoundation框架下的)

        //Asset 资源 可以是图片音频视频

        AVAsset *asset=[AVAsset assetWithURL:url];

        //设置压缩的格式

        AVAssetExportSession *session=[AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetMediumQuality];//mediumquality:中等质量

        //设置导出路径

        NSString *path=[NSTemporaryDirectory() stringByAppendingPathComponent:@"needAV.mov"];

        //创建文件管理类 导出失败,删除已经导出的

        NSFileManager *manager=[[NSFileManager alloc]init];

        //删除已经存在的

        [manager removeItemAtPath:path error:NULL];

        //设置到处路径

        session.outputURL=[NSURL fileURLWithPath:path];

        //设置输出文件的类型

        session.outputFileType=AVFileTypeQuickTimeMovie;

        //开辟子线程处理耗时操作

        [session exportAsynchronouslyWithCompletionHandler:^{

            NSLog(@"导出完成!路径:%@",path);

        }];

        

        

    }

     

     

     

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

    }

     

    @end

     

     

     

    //参考资料 http://www.cnblogs.com/wanghuaijun/p/5575255.html。

  • 相关阅读:
    从零开始学android -- dialog
    Java学习笔记之equals和Objects.equals
    windows svchost.exe 引起的出现的莫名其妙的窗口失去焦点
    android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
    从零开始搭建android框架系列(转)
    js-ES6学习笔记-编程风格(1)
    js-ES6学习笔记-module(4)
    js-ES6学习笔记-module(3)
    js-ES6学习笔记-module(2)
    js-ES6学习笔记-module(1)
  • 原文地址:https://www.cnblogs.com/soulDn/p/5731937.html
Copyright © 2020-2023  润新知