iOS图片压缩
2016-09-26 本文已影响647人
U9995
对于“压”功能,我们可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation方法实现
第一个参数是图片对象本身,第二个参数是压的系数,其值范围为(0-1);
NSData *imgData = UIImageJPEGRepresentation(image, 0.5);
图片“缩”处理:
主要通过 [image drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)]; 实现。
/**
* 1, 按图片最大边成比例缩放图片
*
* @param image 图片
* @param maxSize 图片的较长那一边目标缩到的(宽度/高度)
*
* @return 等比缩放后的图片
*/
- (UIImage *)scaleImage:(UIImage *)image maxSize:(CGFloat)maxSize {
NSData *data = UIImageJPEGRepresentation(image, 1.0);
if(data.length < 200 * 1024){//0.25M-0.5M(当图片小于此范围不压缩)
return image;
}
CGFloat imageWidth = image.size.width;
CGFloat imageHeight = image.size.height;
CGFloat targetWidth = imageWidth;
CGFloat targetHeight = imageHeight;
CGFloat imageMaxSize = MAX(imageWidth, imageHeight);
if (imageMaxSize > maxSize) {
CGFloat scale = 0;
if (imageWidth >= imageHeight) {// 宽长
scale = maxSize / imageWidth;
targetWidth = maxSize;
targetHeight = imageHeight * scale;
} else { // 高长
scale = maxSize / imageHeight;
targetHeight = maxSize;
targetWidth = imageWidth * scale;
}
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
[image drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
return image;
}
/**
* 2, 图片支持等比缩放
*
* @param image 图片
* @param maxSize 缩放比例(通常0~1之间)
*
* @return 等比缩放后的图片
*/
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize {
UIGraphicsBeginImageContext(CGSizeMake(image.size.width *scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
/**
* 3, 等比缩放成自定长宽的图片
*
* @param image 源图片
* @param targetSize 自定义目标图片的size
*
* @return 处理后图片
*/
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {
UIGraphicsBeginImageContext(CGSizeMake(targetSize.width, targetSize.height));
[image drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
UIImage *targetSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return targetSizeImage;
}
//根据图片附获取图片大小(多少M)方法
- (NSData *)imageData:(UIImage *)image {
NSData *data = UIImageJPEGRepresentation(image, 1.0);
if (data.length > 100*1024) {
if (data.length > 1024*1024) {//1M以及以上
data = UIImageJPEGRepresentation(image, 0.1);
}else if (data.length > 512*1024) {//0.5M-1M
data = UIImageJPEGRepresentation(image, 0.5);
}else if (data.length > 200*1024) {//0.25M-0.5M
data = UIImageJPEGRepresentation(image, 0.9);
}
}
return data;
}
原图大小为24.9M, 4288 * 2848像素;经一次UIImageJPEGRepresentation“压“处理
UIImage *tmpImage = [UIImage imageWithContentsOfFile:photo.photoPath];NSData *imageData = UIImageJPEGRepresentation(tmpImage, 0.5);
图片大小为836.9K, 4288 * 2848像素;
经一次UIImagePNGRepresentation“压“处理
UIImage *tmpImage = [UIImage imageWithContentsOfFile:photo.photoPath];NSData *imageData = UIImagePNGRepresentation(tmpImage);
图片大小为24.9M, 4288 * 2848像素;几乎没变
经一次“缩“处理(笔者给予图片最大的宽度限定为640像素)
UIImage *tmpImage = [UIImage imageWithContentsOfFile:photo.photoPath];tmpImage = [self scaleImage:tmpImage maxSize:640];
图片大小为795.9K, 640 * 426像素;
经一次“压缩”处理:
UIImage *tmpImage = [UIImage imageWithContentsOfFile:photo.photoPath];tmpImage = [self scaleImage:tmpImage maxSize:640];NSData *imageData = UIImageJPEGRepresentation(tmpImage, 0.5);
图片大小为698K, 640 * 426像素;大家可以根据实际调整不同参数以达到自己的项目要求。