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图片压缩处理
理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...
随机推荐
- 前端发展态势 && 前端工作流程个人浅析
于在未开启cleartype的情况下,一些中文字体在非偶数字号下的显示效果欠佳,所以一般建议使用12.14.16.18.22px等偶数字号.也就 是对某个分辨率选择离它最近的偶数字号.例如:屏幕横向分 ...
- 禁止Linux系统被 ping
echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf sysctl -p 生效 开启ping功能: 删除 ...
- Hibernate框架注解
1.使用Hibernate注解的步骤 1.添加jar包 Hibernate-annotations.jar ...
- Selenium 使用css selector (资源来源于网络)
Selenium - CSS Selector 昨天我练习了用CSS(即层叠样式表Cascading Stylesheet) Selector来定位(locate)页面上的元素(Elements).S ...
- webots自学笔记(一)软件界面和简单模型仿真
本人是某非理工类某高校大四狗,由于毕设研究需要使用webots软件,在学习使用webots的过程花费了很多时间.由于这个软件基本没有什么中文资料,所以想把自己所学到的一些东西写下来,如有什么错误的地方 ...
- html、css、js实现轮播图
2017-03-13 今天把轮播图的知识1过了一下,写了一个比较简单的轮播图,给大家参考一下. 查看具体的效果点击这个链接 : http://gjhnstxu.me/%E8%BD%AE%E6%92%A ...
- java-信息安全(四)-数据签名、数字证书
概述 信息安全基本概念: 数字签名 数字证书 数字签名 数字签名(又称公钥数字签名.电子签章)是一种类似写在纸上的普通的物理签名,但是使用了公钥加密领域的技术实现,用于鉴别数字信息的方法.一套数字签名 ...
- Broken pipe错误终极解释
叙述 想必或多或少在Java的服务器都会遇到过这种异常,如下图 由于Java偏上层,日常开发接触系统底层的机会偏少,要搞清楚什么原因导致的这种异常,肯定是先要百度google一番. 网络 ...
- jQuery中$.extend(true,object1, object2);深拷贝对象
语法:jQuery.extend( [deep ], target, object1 [, objectN ] ) 深浅拷贝对应的参数就是[deep],是可选的,为true或false.默认情况是fa ...
- NSTimer的精确度
1.iOS中一般UI上面常用两种定时器 NSTimer和CADisplayLink,那么它们分别的精确度是如何呢? CADisplayLink 是用于帧刷新定时器,也就是和界面的刷新率保持一致,理想情 ...