123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // ImageHandlerObject.m
- // MIT_Shop
- //
- // Created by 翟玉磊 on 2018/1/26.
- // Copyright © 2018年 翟玉磊. All rights reserved.
- //
- #import "ImageHandlerObject.h"
- #define KCompressibilityFactor 1280.0f
- @implementation ImageHandlerObject
- + (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
- // Compress by quality
- CGFloat compression = 1;
- NSData *data = UIImageJPEGRepresentation(image, compression);
- if (data.length < maxLength) return image;
-
- CGFloat max = 1;
- CGFloat min = 0;
- for (int i = 0; i < 6; ++i) {
- compression = (max + min) / 2;
- data = UIImageJPEGRepresentation(image, compression);
- if (data.length < maxLength * 0.9) {
- min = compression;
- } else if (data.length > maxLength) {
- max = compression;
- } else {
- break;
- }
- }
- UIImage *resultImage = [UIImage imageWithData:data];
- if (data.length < maxLength) {
- return resultImage;
- }
-
- // Compress by size
- NSUInteger lastDataLength = 0;
- while (data.length > maxLength && data.length != lastDataLength) {
- lastDataLength = data.length;
- CGFloat ratio = (CGFloat)maxLength / data.length;
- CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
- (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
- UIGraphicsBeginImageContext(size);
- [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
- resultImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- data = UIImageJPEGRepresentation(resultImage, compression);
- }
-
- return resultImage;
- }
- #pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法
- + (UIImage *)getJPEGImagerImg:(UIImage *)image{
-
- CGFloat oldImg_WID = image.size.width;
- CGFloat oldImg_HEI = image.size.height;
- //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
- if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){
- //超过设置的最大宽度 先判断那个边最长
- if(oldImg_WID > oldImg_HEI){
- //宽度大于高度
- oldImg_HEI = (KCompressibilityFactor * oldImg_HEI)/oldImg_WID;
- oldImg_WID = KCompressibilityFactor;
- }else{
- oldImg_WID = (KCompressibilityFactor * oldImg_WID)/oldImg_HEI;
- oldImg_HEI = KCompressibilityFactor;
- }
- }
- UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
- NSData *dJpeg = nil;
- if (UIImagePNGRepresentation(newImg)==nil) {
- dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
- }else{
- dJpeg = UIImagePNGRepresentation(newImg);
- }
- return [UIImage imageWithData:dJpeg];
- }
- #pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法
- + (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr{
- NSMutableArray *newImgArr = [NSMutableArray new];
- for (int i = 0; i<imageArr.count; i++) {
- UIImage *newImg = [self getJPEGImagerImg:imageArr[i]];
- [newImgArr addObject:newImg];
- }
- return newImgArr;
- }
- #pragma mark - 压缩一张图片 自定义最大宽高
- + (UIImage *)getJPEGImagerImg:(UIImage *)image compressibilityFactor:(CGFloat)compressibilityFactor{
- CGFloat oldImg_WID = image.size.width;
- CGFloat oldImg_HEI = image.size.height;
- //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
- if(oldImg_WID > compressibilityFactor || oldImg_HEI > compressibilityFactor){
- //超过设置的最大宽度 先判断那个边最长
- if(oldImg_WID > oldImg_HEI){
- //宽度大于高度
- oldImg_HEI = (compressibilityFactor * oldImg_HEI)/oldImg_WID;
- oldImg_WID = compressibilityFactor;
- }else{
- oldImg_WID = (compressibilityFactor * oldImg_WID)/oldImg_HEI;
- oldImg_HEI = compressibilityFactor;
- }
- }
- UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
- NSData *dJpeg = nil;
- if (UIImagePNGRepresentation(newImg)==nil) {
- dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
- }else{
- dJpeg = UIImagePNGRepresentation(newImg);
- }
- return [UIImage imageWithData:dJpeg];
- }
- #pragma mark - 压缩多张图片 自定义最大宽高
- + (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr compressibilityFactor:(CGFloat)compressibilityFactor{
- NSMutableArray *newImgArr = [NSMutableArray new];
- for (int i = 0; i<imageArr.count; i++) {
- UIImage *newImg = [self getJPEGImagerImg:imageArr[i] compressibilityFactor:compressibilityFactor];
- [newImgArr addObject:newImg];
- }
- return newImgArr;
- }
- #pragma mark - 根据宽高压缩图片
- + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
- UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
- UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- @end
|