iOS图片压缩
项目中常会遇到,上传图片的操作,由于iPhone手机直接拍照的图片往往比较大,一般3-4M,如果直接上传不做处理会浪费用户很多流量,再者有很多场景并不需要高清图片,所以在上传图片前对图片进行压缩,是很有必要的。
1.OC中的UIKit中提供了现成的压缩函数 UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality) ,但压缩比率只能是0.1到0.9,如果图片过大,还是无法达到我们想要的效果。
2.对于大图片(10M以上),我们可以先对图片进行裁剪,然后再压缩。这个方法能大大压缩图片的大小。以我的项目中需求为例,需求是:不管多大图片,都要压缩至50kb左右才可以上传到服务器,而且像素不能过度失真。
+ (NSData *)compressWithOrgImg:(UIImage *)img
{ NSData *imageData = UIImageJPEGRepresentation(img, );
float length = imageData.length;
length = length/;
NSLog(@"压缩前的大小:%fKB",length);
// 裁剪比例
CGFloat cout = 0.5; // 压缩比例
CGFloat imgCout = 0.1;
if(length > ){ // 25M以上的图片
cout = 0.1;
imgCout = ;
}else if(length > ){ // 10M以上的图片
cout = 0.2;
imgCout = ;
}else if (length > ) { // 5M以上的图片
cout = 0.3;
imgCout = ;
}else if (length > ) { // 如果原图大于1.5M就换一个压缩级别
cout = 0.7;
imgCout = 0.1;
}else if (length > ) {
cout = 0.8;
imgCout = 0.2;
}else if (length > ) {
cout = 0.8;
imgCout = 0.3;
}else if (length >){ // 小于500k的不用裁剪 imageData = UIImageJPEGRepresentation(img, / imageData.length);
float length = imageData.length;
length = length/;
NSLog(@"压缩后的大小:%fKB",length);
return imageData;
}else{ imageData = UIImageJPEGRepresentation(img, 0.5);
float length = imageData.length;
length = length/;
NSLog(@"压缩后的大小:%fKB",length);
return imageData;
} // 按裁剪比例裁剪
UIImage *compressImage = [img imageByScalingAndCroppingForSize:CGSizeMake(img.size.width * cout, img.size.height *cout)]; // 那压缩比例压缩
imageData = UIImageJPEGRepresentation(compressImage, imgCout); length= imageData.length / ;
NSLog(@"裁剪比例:%f,压缩比例:%f,压缩后的大小:%fKB",cout,imgCout,length);
return imageData;
}
// 裁剪
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height; if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor; // center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
} UIGraphicsBeginImageContext(targetSize); // this will crop CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width= scaledWidth;
thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image"); //pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
代码中针对不同大小图片,给出了不同的压缩比率,以保证压缩后的图片大小都在50k左右。此处的“裁剪”是将图片的宽高等比例缩小到指定的比率
iOS图片压缩的更多相关文章
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...
- iOS 图片压缩方法
iOS 图片压缩方法 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size). 压缩图片质量 NSData *data = UIImageJPEGReprese ...
- iOS图片压缩上传
本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- iOS图片压缩处理
理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...
- iOS图片压缩问题
对于压缩的处理我给出的建议是 先判断 图片的大小,如果是本地图片最好用nsfilemanager 来判断 .如果不能用这个判断的话 就只能先将图片转成data,然后再判断了. 图片转成data 当然就 ...
- iOS 图片压缩
+ (UIImage *)scaleFromImage:(UIImage *)image { CGSize newSize = CGSizeMake(366, 366); //目标图片的大小 ...
- jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...
- js 前端图片压缩+ios图片角度旋转
step1:读取选择的图片,并转为base64: function ImgToBase64 (e, fn) { // 图片方向角 //fn为传入的方法函数,在图片操作完成之后执行 var Orient ...
- 图片上传前 压缩,base64图片压缩 Exif.js处理ios拍照倒置等问题
曾写过在前端把图片按比例压缩不失真上传服务器的前端和后台,可惜没有及时做总结保留代码,只记得js利用了base64位压缩和Exif.js进行图片处理,还有其中让我头疼的ios拍照上传后会倒置等诸多问题 ...
随机推荐
- js 时间函数 及相关运算大全
js 时间函数 及相关运算大全 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- iOS获取键盘的高度
- oracle中有关用户、角色的一些概念。
oracle中的每个用户对应一个单独的方案(schema),方案的名字与用户名一样,方案中包含很多数据对象,表,视图,触发器,存储过程等元素. oracle中管理数据库的角色有sys,system,数 ...
- 测试一下MarkDown
欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接 ...
- 在eclipse中安装activiti插件
最近在学习activiti,先学习安装插件吧. 单击help->Install new Software 然后添加资源 name:activiti location:http://activit ...
- 此集合已经采用方案 http 的地址。此集合中每个方案中最多只能包含一个地址。
错误信息:此集合已经采用方案 http 的地址.此集合中每个方案中最多只能包含一个地址.如果服务承载于 IIS 中,则可以通过将“system.serviceModel/serviceHostingE ...
- 大数A-B
还没写过大数减法,今天比赛还WA了两次... #include<iostream> #include<string> using namespace std; void sub ...
- mysql中的timestamp类型时间比较:unix_timestamp函数
在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...
- closest
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 简单的javascript例子
<html> <head> <title>hongmaju</title> <link rel="shortcut icon" ...