转自: http://www.cnblogs.com/cocoajin/p/3494290.html

简介:

使用 iOS 系统 UIImagePickerController

  • 获取视频大小
  • 获取视频长度
    - (void)viewDidLoad
    {[super viewDidLoad];// Do any additional setup after loading the view.self.title = @"AVVideo Demo";CGRect theFrame = CGRectMake(10, 15, 130, 20);[self addOpBtnWith:@"pick Video" frame:theFrame action:@selector(pickVideoTaped)];theFrame = CGRectMake(150, 15, 130, 20);[self addOpBtnWith:@"转成mp4" frame:theFrame action:@selector(revoverToMp4)];}#pragma mark action//转换格式为 mp4
    - (void)revoverToMp4
    {AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:_videoURL options:nil];NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]){AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAssetpresetName:AVAssetExportPresetHighestQuality];NSDateFormatter* formater = [[NSDateFormatter alloc] init];[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];NSString* _mp4Path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]];exportSession.outputURL = [NSURL fileURLWithPath: _mp4Path];exportSession.shouldOptimizeForNetworkUse = YES;exportSession.outputFileType = AVFileTypeMPEG4;[exportSession exportAsynchronouslyWithCompletionHandler:^{switch ([exportSession status]) {case AVAssetExportSessionStatusFailed:{UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error"message:[[exportSession error] localizedDescription]delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];[alert show];break;}case AVAssetExportSessionStatusCancelled:NSLog(@"Export canceled");break;case AVAssetExportSessionStatusCompleted:NSLog(@"Successful!");NSLog(@"%@",_mp4Path);NSLog(@"file Size %ldk",(long)[self getFileSize:_mp4Path]);break;default:break;}}];}else{UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error"message:@"AVAsset doesn't support mp4 quality"delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];[alert show];}}//弹出视频拍摄- (void)pickVideoTaped
    {pickVideo = [[UIImagePickerController alloc]init];pickVideo.sourceType = UIImagePickerControllerSourceTypeCamera;NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];pickVideo.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];pickVideo.delegate = self;[self presentViewController:pickVideo animated:YES completion:nil];}
    #pragma mark - UIImagePickerControllerDelegate//拍照完成代理
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {NSLog(@"%@",info);_videoURL = info[UIImagePickerControllerMediaURL];NSString * size = [NSString stringWithFormat:@"%ld kb", (long)[self getFileSize:[[_videoURL absoluteString] substringFromIndex:16]]];NSString *length = [NSString stringWithFormat:@"%.0f s", [self getVideoDuration:_videoURL]];NSLog(@"-------------%@%@",size,length);[picker dismissViewControllerAnimated:YES completion:nil];}//取消代理- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {[picker dismissViewControllerAnimated:YES completion:nil];
    }//获取视频时间
    - (CGFloat) getVideoDuration:(NSURL*) URL
    {NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]forKey:AVURLAssetPreferPreciseDurationAndTimingKey];AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:URL options:opts];float second = 0;second = urlAsset.duration.value/urlAsset.duration.timescale;return second;
    }//获取视频 大小
    - (NSInteger) getFileSize:(NSString*) path
    {NSFileManager * filemanager = [[NSFileManager alloc]init];if([filemanager fileExistsAtPath:path]){NSDictionary * attributes = [filemanager attributesOfItemAtPath:path error:nil];NSNumber *theFileSize;if ( (theFileSize = [attributes objectForKey:NSFileSize]) )return  [theFileSize intValue]/1024;elsereturn -1;}else{return -1;}
    }- (void)addOpBtnWith:(NSString *)name frame:(CGRect)frame action:(SEL)action
    {UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];aBtn.frame = frame;[aBtn setTitle:name forState:UIControlStateNormal];[aBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:aBtn];
    }

iOS开发-UI控件:UIImagePickerController 视频录制操作,视频大小,时间长度相关推荐

  1. IOS开发UI控件UIScrollView和Delegate的使用

    1. 什么是UIScrollView 移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限 当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容 普通的UIVie ...

  2. iOS基础——UI控件之UIAlertController、UINavigationController、Segue、SVProgressHUD

    iOS基础--UI控件之UIAlertController.UINavigationController.Segue 一.UIAlertController 1.普通对话框 -(void)update ...

  3. IOS 常用UI控件

    目录 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 具体内容 下拉刷新 EGOTableViewPullRefresh ...

  4. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

  5. sketch里的ios控件_30个让你眼前一亮的iOS Swift UI控件!

    前言 笔者接触 iOS 开发有一段时间了,尤其特别喜欢UI部分,特意收集整理了30个让你惊艳的第三方开源控件(swift),无论是应用到项目中还是用来学习都能让你大呼过瘾,废话不多说,直接上图上链接! ...

  6. 封装iOS原生UI 控件给RN调用

    封装原生UI控件给RN调用 前言 前些日子在做项目的时候,接到一个需求:在APP上,点击一个Cell,跳转到拍照页面进行拍照.按理来说,这个需求实现起来,并不困难,第一想法是想直接用 UIImageP ...

  7. iOS基本UI控件总结

    包括以下几类: //继承自NSObject:(暂列为控件) UIColor *_color;    //颜色 UIImage *_image;    //图像 //继承自UIView:只能相应手势UI ...

  8. iOS开发图片控件(ImageView)绑定代码并显示一张图片

    我在之前的一篇博客<iOS中如何显示一张图片>中谈到如何使用Image View来显示图片,这个操作是直接在Main.storyboard实现的.也就是说这张图片是写死在界面中了.但是在以 ...

  9. IOS开发UIScrollView控件详解

    首先实现UIScrollViewDelegate协议: [plain] view plaincopy #import <UIKit/UIKit.h> @interface Activity ...

最新文章

  1. 追梦童年,80后记忆中的Gameboy,教你怎么用模拟器去重温旧梦!
  2. idea插件手动安装
  3. 限定虚拟机可用的CPU利用率
  4. maven工程下 读取resource下配置文件
  5. 基于 Roslyn 实现动态编译
  6. git 历史操作日志_Git - 查看提交历史
  7. 【gRPC基础知识】快速部署
  8. canal 使用注意事项
  9. 目标检测相关概念:IOU,precision, recall, AP, mAP
  10. VC6++配置汇编环境和第一个简单程序
  11. 德州农工大学计算机专业研究生,德州农工大学计算机专业
  12. 计算机学院杨洋,杨洋-地球科学与技术学院
  13. Ant学习笔记(Ant入门)
  14. c#dataview遍历_[C#] DataView用法
  15. 共振峰检测matlab,基于 LPC 系数的共振峰估计
  16. 什么是SOA?什么是ESB?及其在银行业中是如何发展的?
  17. 不安的心,从离职到创业
  18. 在LINUX下怎么播放MP3等音频文件啊?
  19. python机器学习实战6:利用adaBoost元算法提高分类性能
  20. 【python】解决TypeError: ‘str‘ object cannot be interpreted as an integer

热门文章

  1. 20220707拖把更名器的正则表达式的使用
  2. CPU性能优化:Cache
  3. mysql数据库搜索引擎要先进入_2020尔雅通识课《MYSQL数据库基础加讲义》章测试答案高校邦《搜索引擎优化技术》完整免费标准答案...
  4. CNN卷积神经网络结构
  5. Swift Jama Matrix线性代数矩阵运算库
  6. greenplum6数据库安装部署以及扩容介绍
  7. 富士康宣布投资3.42亿美元研发人工智能
  8. 淘宝天猫京东考拉唯品会拼多多美团实现查券返利微信公众号云函数
  9. 2022年[最新]泰晤士高等教育世界大学声誉排名(完整版)
  10. 林丹 - 没有人想永远输给你