项目中常会遇到,上传图片的操作,由于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图片压缩的更多相关文章

  1. UIImage 和 iOS 图片压缩UIImage / UIImageVIew

    UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...

  2. iOS 图片压缩方法

    iOS 图片压缩方法 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size). 压缩图片质量 NSData *data = UIImageJPEGReprese ...

  3. 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 ...

  4. iOS图片压缩处理

    理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...

  5. iOS图片压缩问题

    对于压缩的处理我给出的建议是 先判断 图片的大小,如果是本地图片最好用nsfilemanager 来判断 .如果不能用这个判断的话 就只能先将图片转成data,然后再判断了. 图片转成data 当然就 ...

  6. iOS 图片压缩

    + (UIImage *)scaleFromImage:(UIImage *)image {    CGSize newSize = CGSizeMake(366, 366);   //目标图片的大小 ...

  7. jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)

    上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...

  8. js 前端图片压缩+ios图片角度旋转

    step1:读取选择的图片,并转为base64: function ImgToBase64 (e, fn) { // 图片方向角 //fn为传入的方法函数,在图片操作完成之后执行 var Orient ...

  9. 图片上传前 压缩,base64图片压缩 Exif.js处理ios拍照倒置等问题

    曾写过在前端把图片按比例压缩不失真上传服务器的前端和后台,可惜没有及时做总结保留代码,只记得js利用了base64位压缩和Exif.js进行图片处理,还有其中让我头疼的ios拍照上传后会倒置等诸多问题 ...

随机推荐

  1. Remote Direct Memory Access (RDMA)

    RDMA有三类实现方式,包括RoCE,iWARP和InfiniBand.RDMA的基础是Virtual Interface Architechure (VIA). 参考文档: https://en.w ...

  2. Hive的Metastore contains multiple versions

    hive 客户端报错:Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeExcepti ...

  3. 对Gearman中client,worker,jobserver的理解

    在gearman的官网http://gearman.org/有以下的一段说明 A Gearman powered application consists of three parts: a clie ...

  4. Eclipse插件卸载

            以前搞过安卓,重装系统后,安卓损坏了,每次还会提示那个窗口很烦人.       使用Eclipse自带的卸载插件功能即可,Help->About Eclipse->Inst ...

  5. 关于@synchronized(self)的用法

    @synchronized 的作用是创建一个互斥锁,保证此时没有其它线程对self对象进行修改.这个是objective-c的一个锁定令牌,防止self对象在同一时间内被其它线程访问,起到线程的保护作 ...

  6. hdu 5067 Harry And Dig Machine

    http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...

  7. 【HDOJ】3221 Brute-force Algorithm

    归来吧很好推导.T(n) = a^f(n-1)*b^f(n)%p.主要难点在于求mod和fibo.引用如下公式A^B%C = A^(B%phi(C) + phi(C))%C, 满足B>=phi( ...

  8. codeforce --- 340D

    D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Linux学习笔记2——Linux中常用文件目录操作命令

    ls 显示文件和目录列表 -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 mkdir 创建目录 -p 父目录不存在情况下先生成父目录 cd 切换目录 touch 生成一个空文件 e ...

  10. Tree2cycle

    Problem Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, w ...