zoukankan      html  css  js  c++  java
  • 视频压缩

    //原理,还是调用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

  • 相关阅读:
    前台开发——处理用户密码登录的修改功能
    前台开发——处理用户收货信息的修改
    Max Sum -- hdu -- 1003
    (深搜)Oil Deposits -- hdu -- 1241
    (博弈 sg入门2)
    (博弈 sg入门)kiki's game -- hdu -- 2147
    (巴什博弈 sg函数入门1) Brave Game -- hdu -- 1846
    (全排列)Ignatius and the Princess II -- HDU -- 1027
    (模拟)Arithmetic Sequence -- HDU -- 5400
    (字符串 键盘转换)Convert QWERTY to Dvorak -- zoj -- 5526
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4665319.html
Copyright © 2011-2022 走看看