ios 视频/图片压缩
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString*path=[[NSBundle mainBundle]pathForResource:@"dachao" ofType:@"m4v"];
NSString*outpath=[NSString stringWithFormat:@"%@/%@/%@",NSHomeDirectory(),@"Documents",@"dachaotest"];
NSLog(@"%@",NSHomeDirectory());
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"dateString:%@",dateString);
[self lowQuailtyWithInputURL:[[NSURL alloc]initFileURLWithPath:path] outputURL:[NSURL fileURLWithPath:outpath] blockHandler:^(AVAssetExportSession *session) {
if (session.status==AVAssetExportSessionStatusCompleted) {
NSLog(@"complete");
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"dateString:%@",dateString);
}else{
NSLog(@"fail");
}
}];
NSLog(@"代码结束");
}
//视频压缩代码
- (void) lowQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
blockHandler:(void (^)(AVAssetExportSession*))handler
{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
session.outputURL = outputURL;
//压缩格式
session.outputFileType = AVFileTypeQuickTimeMovie;
[session exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(session);
}];
}
//图片等比例压缩 有损压缩
-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(size);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
NSLog(@"scale image fail");
}
UIGraphicsEndImageContext();
return newImage;
}
//图片无损压缩
-(UIImage*)imageCompressWithImage:(UIImage*)image andQuality:(float)ql{
NSData*data=UIImageJPEGRepresentation(image, ql);
UIImage*newImage=[UIImage imageWithData:data];
data=nil;
return newImage;
}
视频压缩格式:
/*!
@constant AVFileTypeQuickTimeMovie
@abstract A UTI for the QuickTime movie file format.
@discussion
The value of this UTI is @"com.apple.quicktime-movie".
Files are identified with the .mov and .qt extensions.
*/
AVF_EXPORT NSString *const AVFileTypeQuickTimeMovie NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeMPEG4
@abstract A UTI for the MPEG-4 file format.
@discussion
The value of this UTI is @"public.mpeg-4".
Files are identified with the .mp4 extension.
*/
AVF_EXPORT NSString *const AVFileTypeMPEG4 NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeAppleM4V
@discussion
The value of this UTI is @"com.apple.m4v-video".
Files are identified with the .m4v extension.
*/
AVF_EXPORT NSString *const AVFileTypeAppleM4V NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeAppleM4A
@discussion
The value of this UTI is @"com.apple.m4a-audio".
Files are identified with the .m4a extension.
*/
AVF_EXPORT NSString *const AVFileTypeAppleM4A NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileType3GPP
@abstract A UTI for the 3GPP file format.
@discussion
The value of this UTI is @"public.3gpp".
Files are identified with the .3gp, .3gpp, and .sdv extensions.
*/
AVF_EXPORT NSString *const AVFileType3GPP NS_AVAILABLE(10_11, 4_0);
压缩视频有三级:
AVF_EXPORT NSString *const AVAssetExportPresetLowQuality NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetMediumQuality NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetHighestQuality NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset640x480 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset960x540 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset1280x720 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset1920x1080 NS_AVAILABLE(10_7, 5_0);
AVF_EXPORT NSString *const AVAssetExportPreset3840x2160 NS_AVAILABLE(10_10, 9_0);
经测试:
iPhone6高品质视频:一分钟 57mb 中品质压缩后:6.7mb 低品质压缩后:1.4mb 建议压缩:中品质。压缩时间大约1分钟
同品质之间的压缩不会压缩大小,时间也相对短,如对中品质视频进行中品质压缩后,其大小不变,时长大约20s;
另外:
视频的传入传出的路径为file:格式 用:[NSURL fileURLWithPath:outpath];
ios 视频/图片压缩的更多相关文章
- iOS学习——图片压缩到指定大小以内
一.图片压缩简述 在我们开发过程中,有可能会遇到拍照.或者从相册中选择图片,要么单选或者多选,然后上传图片到服务器,一般情况下一张图片可能3-4M,如果类似微信朋友圈上传9张图片大约是 35M左右,如 ...
- iOS 视频选择压缩
//原理,还是调用UIImagePickerController控制器,设置Type为视频 #import "ViewController.h" #import <AVFou ...
- IOS 视频.图片上传服务器
//上传视频 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer. ...
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...
- IOS 视频分解图片、图片合成视频
在IOS视频处理中,视频分解图片和图片合成视频是IOS视频处理中经常遇到的问题,这篇博客就这两个部分对IOS视频图像的相互转换做一下分析. (1)视频分解图片 这里视频分解图片使用的是AVAssetI ...
- ios里面如何压缩图片
在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...
- iOS 图片压缩方法
iOS 图片压缩方法 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size). 压缩图片质量 NSData *data = UIImageJPEGReprese ...
- 图片上传前 压缩,base64图片压缩 Exif.js处理ios拍照倒置等问题
曾写过在前端把图片按比例压缩不失真上传服务器的前端和后台,可惜没有及时做总结保留代码,只记得js利用了base64位压缩和Exif.js进行图片处理,还有其中让我头疼的ios拍照上传后会倒置等诸多问题 ...
- iOS图片压缩处理
理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...
随机推荐
- ORM 实现数据库表的增删改查
这次通过反射技术来实现一下数据库表的增删改查对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 注:引用时约束了以下几点: 数据 ...
- Laravel的ORM入门
源码目录在\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations下 关系:一对多(One To Many) 场景:每篇 ...
- 【2017-2-17】VS基本应用及C#基础第一节(定义变量、输入及输出)
一VS基本应用 (一)新建项目 新建项目可有多种方法例如: 1. 在VS起始页面建立新项目 2. 在集成环境中,通过"文件"/"新建"/"项目&q ...
- 每天一个Linux命令 4
Linux系统关机命令 shutdown -h 时间 init 0 poweroff Linux系统注销命令 logout 或者是快捷键 Ctrl+d Linux系统重启命令 reboot Linux ...
- 1642: [Usaco2007 Nov]Milking Time 挤奶时间
1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 582 Solved: 33 ...
- 首个写博客的Android任务
任务1 单击按钮图片选择器 使用TextView,RadioGroup,RadioButton完成. 设置单击按钮选择显示花朵. 首先设置了页面布局 <LinearLayout xmlns:an ...
- RabbitMQ-从基础到实战(5)— 消息的交换(下)
转载请注明出处 0.目录 RabbitMQ-从基础到实战(1)- Hello RabbitMQ RabbitMQ-从基础到实战(2)- 防止消息丢失 RabbitMQ-从基础到实战(3)- 消息的交换 ...
- iOS开发之Run Loop
1.概述 (1) Run Loop提供了一种异步执行代码的机制,不能并行执行任务. (2) 在主队列中,Main Run Loop直接配合任务的执行,负责处理UI事件.计时器,以及其它内核相关事件. ...
- vue学习笔记 样式 class style(五)
使用v-bind数据绑定class和style,v-bind:class可以与传统的class属性共存,其中可以用{}设置多个class,根据条件判断的语法是class名:条件,带-的class名需要 ...
- dotweb——go语言的一个微型web框架(二)启动dotweb
以上的代码截图表示启动一个dotweb服务,在浏览器里输入127.0.0.1:8080,将会得到一个"index"的页面. app := dotweb.New() dotweb.N ...