/**
图片宽高尺寸限制在200-1024之间,maxSize kb以内
*/
- (NSData *)resetSizeOfImage:(UIImage *)sourceImage maxSize:(NSInteger)maxSize {
//最终图片data
__block NSData *finallImageData = UIImageJPEGRepresentation(sourceImage, 1.0);
//原图宽高比
CGFloat sourceImageAspectRatio = sourceImage.size.width / sourceImage.size.height;
CGFloat defaultWidth = sourceImage.size.width;
CGFloat defaultHeight = sourceImage.size.height;
CGSize defaultSize = sourceImage.size;
if (defaultWidth > 1024 || defaultHeight > 1024) {
//判断尺寸是否过大
if (defaultWidth > 1024 && defaultWidth > defaultHeight) {
defaultSize = CGSizeMake(1024, 1024 / sourceImageAspectRatio);
} else if (defaultHeight > 1024 && defaultWidth < defaultHeight) {
defaultSize = CGSizeMake(1024 * sourceImageAspectRatio, 1024);
}
} else if (defaultWidth < 200 || defaultHeight < 200) {
//判断尺寸是否过小
if (defaultWidth < 200 && defaultWidth < defaultHeight) {
defaultSize = CGSizeMake(200, 200 / sourceImageAspectRatio);
} else if (defaultHeight < 200 && defaultWidth > defaultHeight) {
defaultSize = CGSizeMake(200 * sourceImageAspectRatio, 200);
}
}
//先调整分辨率
UIImage *newImage = [self newSizeImage:defaultSize image:sourceImage];
finallImageData = UIImageJPEGRepresentation(newImage, 1.0);
//判断当前质量是否满足要求,不满足再进行压缩
NSUInteger sizeOrigin = finallImageData.length;//data.length即可得到图片的字节大小
NSUInteger sizeOriginKB = sizeOrigin / 1000;
if (sizeOriginKB <= maxSize) {
return finallImageData;
}
//保存压缩系数
NSMutableArray *compressionQualityArr = [NSMutableArray array];
CGFloat avg = 1.0/250;
CGFloat value = avg;
for (int i = 250; i >= 1; i--) {
value = i*avg;
[compressionQualityArr addObject:@(value)];
}
/*
调整大小
说明:压缩系数数组compressionQualityArr是从大到小存储。
*/
//思路:使用二分法搜索
finallImageData = [self halfFuntion:compressionQualityArr image:newImage sourceData:finallImageData maxSize:maxSize];
//如果还是未能压缩到指定大小,则进行降分辨率
while (finallImageData.length == 0) {
//每次降100分辨率
CGFloat reduceWidth = 100.0;
CGFloat reduceHeight = 100.0/sourceImageAspectRatio;
if (defaultSize.width-reduceWidth <= 0 || defaultSize.height-reduceHeight <= 0) {
break;
}
defaultSize = CGSizeMake(defaultSize.width-reduceWidth, defaultSize.height-reduceHeight);
UIImage *image = [self newSizeImage:defaultSize
image:[UIImage imageWithData:UIImageJPEGRepresentation(newImage,[[compressionQualityArr lastObject] floatValue])]];
finallImageData = [self halfFuntion:compressionQualityArr image:image sourceData:UIImageJPEGRepresentation(image,1.0) maxSize:maxSize];
}
return finallImageData;
}
#pragma mark - 调整图片分辨率/尺寸(等比例缩放)
- (UIImage *)newSizeImage:(CGSize)size image:(UIImage *)sourceImage {
CGSize newSize = CGSizeMake(sourceImage.size.width, sourceImage.size.height);
CGFloat tempHeight = newSize.height / size.height;
CGFloat tempWidth = newSize.width / size.width;
if (tempWidth > 1.0 && tempWidth > tempHeight) {
newSize = CGSizeMake(sourceImage.size.width / tempWidth, sourceImage.size.height / tempWidth);
} else if (tempHeight > 1.0 && tempWidth < tempHeight) {
newSize = CGSizeMake(sourceImage.size.width / tempHeight, sourceImage.size.height / tempHeight);
} else {
newSize = size;
}
UIGraphicsBeginImageContext(newSize);
[sourceImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - 二分法
- (NSData *)halfFuntion:(NSArray *)arr image:(UIImage *)image sourceData:(NSData *)finallImageData maxSize:(NSInteger)maxSize {
NSData *tempData = [NSData data];
NSUInteger start = 0;
NSUInteger end = arr.count - 1;
NSUInteger index = 0;
NSUInteger difference = NSIntegerMax;
while(start <= end) {
index = start + (end - start)/2;
finallImageData = UIImageJPEGRepresentation(image,[arr[index] floatValue]);
NSUInteger sizeOrigin = finallImageData.length;
NSUInteger sizeOriginKB = sizeOrigin / 1024;
NSLog(@"当前降到的质量:%ld", (unsigned long)sizeOriginKB);
NSLog(@"\nstart:%zd\nend:%zd\nindex:%zd\n压缩系数:%lf", start, end, (unsigned long)index, [arr[index] floatValue]);
if (sizeOriginKB > maxSize) {
start = index + 1;
} else if (sizeOriginKB < maxSize) {
if (maxSize-sizeOriginKB < difference) {
difference = maxSize-sizeOriginKB;
tempData = finallImageData;
}
if (index<=0) {
break;
}
end = index - 1;
} else {
break;
}
}
return tempData;
}
iOS限制上传图片大小和尺寸
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言:该解决方案只支持IE10及以上,目前来看IE9及以下所占的份额越来越小了,如果在项目不需要考虑IE9及以下的...
- 打包上传尺寸 icon 1024*1024 3.5寸屏:640 * 920 4寸屏:640 * 1136 4.7寸...
- 一、UIWebView 中的 方法可以直接加载 stringByEvaluatingJavaScriptFromS...