在上传图片时,如果对图片内存大小有要求,需上传前进行压缩处理,这样服务器也会减少压力。
接下来我们正式开始:
1、先对图片进行“压”操作,降低图片的分辨率,宽高不会改变。
2、如果还不能满足要求,则在“压”的基础上,再对图片进行“缩”操作,改变图片的宽高。
直接从相机,相册Finish的代理开始
#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
NSData *picData;
// 判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {// 返回为png图像
picData = UIImagePNGRepresentation(image);
} else {// 返回为JPEG图像
picData = UIImageJPEGRepresentation(image, 1.0);
}
NSLog(@"前:%@",[self sizeWithData:picData]);
UIImage *uploadImg;
//判断图片是否超过1M,超过需要压缩处理
if (picData.length > 1*1024*1024) {
//这里保证图片清晰,500的宽差不多
uploadImg = [self compressionWithImg:image andImageWidth:500];
} else {
uploadImg = image;
}
// 进行上传操作 TO DO...
}];
}
// 计算图片大小
- (NSString *)sizeWithData:(NSData *)data {
double dataLength = [data length] * 1.0;
double orgrionLenght = dataLength;
NSArray *typeArray = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB",@"ZB",@"YB"];
NSInteger index = 0;
while (dataLength > 1024) {
dataLength /= 1024.0;
index ++;
}
NSString *str = [NSString stringWithFormat:@"\n%.1f字节,%.3f%@\n",orgrionLenght,dataLength,typeArray[index]];
return str;
}
// 处理方式:先“压”后”缩“
-(UIImage*)compressionWithImg:(UIImage *)imaged andImageWidth:(CGFloat)imageWidth {
//先压,经过这一步后,图片的size其实没有改变的,再缩后才变。
NSData *imgData = UIImageJPEGRepresentation(imaged, 0.5);
imaged = [UIImage imageWithData:imgData];
CGFloat height = imageWidth*(imaged.size.height/imaged.size.width);
UIImage *newImage = [self imageByScalingAndCroppingForSize:CGSizeMake(imageWidth, height) withSourceImage:imaged];
NSData *newImageData = UIImageJPEGRepresentation(newImage, 1.0);
NSLog(@"后:%@",[self sizeWithData:newImageData]);
// 保存图片到沙盒路径下,用于测试
/*
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"image2.png"];
[newImageData writeToFile:path atomically:YES];
*/
return [UIImage imageWithData:newImageData];
}
// “缩”处理
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withSourceImage:(UIImage *)sourceImage {
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;
} else {
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if (widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight-scaledHeight)*0.5;
} else {
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(@"image error");
}
UIGraphicsEndImageContext();
return newImage;
}