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左右。此处的“裁剪”是将图片的宽高等比例缩小到指定的比率

上一篇:基于.net开发chrome核心浏览器【二】


下一篇:iOS图片压缩问题