项目中常会遇到,上传图片的操作,由于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. 如何用OS X的Xcode写C语言程序

    声明:以下内容非本人原创,转载于别处.拿出来只是分享给FY们,不喜勿喷!原创地址http://blog.yorkxin.org/posts/2009/03/15/fundamental-c-with- ...

  2. Codeforces Round #313 (Div. 2)

    大半年没有打Codeforces , 昨天开始恢复打Codeforces, 简直是, 欲语泪先流啊. 手残到爆的写错了范围, 手残的数漏了条件, 简直不能直视, 最坑爹的是, E题没时间写代码了. 题 ...

  3. Contest 20140708 testB dp 组合数

    testB 输入文件: testB.in  输出文件testB.out 时限3000ms 问题描述: 定义这样一个序列(a1,b1),(a2,b2),…,(ak,bk)如果这个序列是方序列的话必须满足 ...

  4. node场景

    http://www.zhihu.com/question/19653241 http://www.csdn.net/article/2012-05-03/2805296 http://limu.it ...

  5. 使用javascript获取网址的各个参数

    有时也挺无奈的,为了实现一个功能,不得不用到前台获取参数.幸亏,有javascript,不然真的是坑大发了,感谢javascript的创造者.开始show大图: 属性    值href:完整的 URL ...

  6. python手记(31)

    #!/usr/bin/env python #-*- coding: utf-8 -*- import cv2 import numpy as np fn="test2.jpg" ...

  7. Oracle数据库12560及更改密码为永久

    小厂子的程序员还要折腾Oracle维护.这这里简单记录一下Oracle数据库服务器使用时遇到并解决的一些问题. 1. 在Oracle数据库服务器上(即安装Oracle数据库端的机器上),可以使用命令 ...

  8. Java第一次写的流布局图形界面,留个纪念

    package jisuanqi; import java.awt.*; public class MyFrame extends Frame{ //继承Frame类 public MyFrame() ...

  9. MVC5 学习整理

    一.概述 MVC简介: •       模型(Model) “数据模型”(Model)用于封装与应用程序的业务逻辑相关的数据以及对数据的处理方法.“模型”有对数据直接访问的权力,例如对数据库的访问.“ ...

  10. JavaScript高级程序设计19.pdf

    注册处理程序 navigator.registerContentHandler("applicat/rss+xml","http://www.somereader.com ...