项目中常会遇到,上传图片的操作,由于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左右。此处的“裁剪”是将图片的宽高等比例缩小到指定的比率

最新文章

  1. Redis配置集群一(window)
  2. OGNl和ValueStack的基础和深入分析
  3. Javascript之UI线程与性能优化
  4. js的replace方法
  5. 【转】c++笔试题
  6. 函数fsp_alloc_seg_inode
  7. postgresql9.4新特性jsonb学习-update更新操作
  8. 用 Swift 编写面向协议的网络请求
  9. 我的Java笔记
  10. Bootsrtap表单
  11. [SCOI2005]栅栏 二分+dfs
  12. SQLServer之修改存储过程
  13. 深入理解Java Stream流水线
  14. maven自动部署项目以及常见问题解决
  15. PyCharm‘s Project Deployment
  16. Scrapy对接selenium+phantomjs
  17. day21-类的组合
  18. 为什么使用this构造器
  19. Android 数据存储03之SQLite
  20. 批处理-通过mono把c#编译成dll

热门文章

  1. mongodb 查看数据库和表大小
  2. Android 每天定时提醒功能实现
  3. C# 哈希表的实现
  4. Azure SQL 数据库的灵活缩放预览版简介
  5. Spark Streaming fileStream实现原理
  6. HDU --3549
  7. [转]Ubuntu上的包管理:dpkg,apt和aptitude
  8. TCP HTTP 详细内存分析 &amp; time_wait setsockopt
  9. C++Primer第5版学习笔记(四)
  10. 通过例子学python(2.2)