发现一个很好用的图片压缩的拓展。将图片压缩成以设定的宽度,高度以图片自己的高度比例缩放,以及压缩图片的数据大小达到低于设定的值。
使用到的地方还是不少,比如分享图文到朋友圈时就有限制图片不大于32K。
#import <UIKit/UIKit.h>
@interface UIImage (Compress)
/**
* 图片压缩:将图片压缩成以640为宽,高度以图片自己的高度比例缩放
*
* @param targetWH 要压缩到的尺寸
* @param maxFileSize 最大图片数据大小
*
* @return 压缩后的图片数据
*/
- (UIImage *)compressionImageToDataTargetWidth:(CGFloat)targetWidth maxFileSize:(NSInteger)maxFileSize;
- (UIImage *)compressImageToScaleWidth:(CGFloat)width;
- (UIImage *)compressImageToScaleSize:(CGSize)size;
+ (UIImage *)scaleImage:(UIImage *)image toKb:(NSInteger)kb;
/// 将图片压缩成以640为宽,高度以图片自己的高度比例缩放
/// 比如图片size == 1280 * 2000, 压缩之后---> 640 * 1000; size == 1280 * 1000, 压缩之后---> 640 * 500
- (NSData *)compressToData;
@end
#import "UIImage+Compress.h"
@implementation UIImage (Compress)
- (UIImage *)compressionImageToDataTargetWidth:(CGFloat)targetWidth maxFileSize:(NSInteger)maxFileSize {
if (targetWidth <= 0) {
targetWidth = 1024;
}
//缩
CGSize newSize = CGSizeMake(self.size.width, self.size.height);
CGFloat tempHeight = newSize.height / targetWidth;
CGFloat tempWidth = newSize.width / targetWidth;
if (tempWidth > 1.0 && tempWidth > tempHeight) {
newSize = CGSizeMake(self.size.width / tempWidth, self.size.height / tempWidth);
}
else if (tempHeight > 1.0 && tempWidth < tempHeight){
newSize = CGSizeMake(self.size.width / tempHeight, self.size.height / tempHeight);
}
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//压
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
NSData *imageData = UIImageJPEGRepresentation(newImage, compression);
while (imageData.length / 1000 > maxFileSize && compression > maxCompression) {
compression -= 0.1;
imageData = UIImageJPEGRepresentation(newImage, compression);
}
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
- (UIImage *)compressImageToScaleSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)compressImageToScaleWidth:(CGFloat)newwidth {
CGSize imageSize = self.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat newHeight = newwidth * height / width;
return [self compressImageToScaleSize:CGSizeMake(newwidth, newHeight)];
}
- (NSData *)compressToData {
if (self.size.width < 640) {
return UIImageJPEGRepresentation(self, 0.9);
}
// 将图片尺寸缩到640 * xxx, `xxx`由图片自身高度决定
UIImage * image = [self compressImageToScaleWidth:640];
return UIImageJPEGRepresentation(image, 0.9);
}
+(UIImage *)scaleImage:(UIImage *)image toKb:(NSInteger)kb{
if (!image) {
return image;
}
if (kb<1) {
return image;
}
kb*=1024;
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
NSData *imageData = UIImageJPEGRepresentation(image, compression);
while ([imageData length] > kb && compression > maxCompression) {
compression -= 0.1;
imageData = UIImageJPEGRepresentation(image, compression);
}
NSLog(@"当前大小:%fkb",(float)[imageData length]/1024.0f);
UIImage *compressedImage = [UIImage imageWithData:imageData];
return compressedImage;
}
@end