在处理图片中由于服务端等等原因我们需要对图片做一个规格处理、比如图片的体积。图片的质量。如果我们只是处理质量而不处理体积这样图片可能即使很大也很模糊等等。。。该内容对图片的压缩采用图片系数压缩。并不是指定的长度宽度压缩。
在现实中需要质量压缩和体积压缩共同使用才能达到效果:
-(UIImage *)imageUserToCompressForSizeImage:(UIImage *)image newSize:(CGSize)size{
UIImage *newImage = nil;
CGSize originalSize = image.size;//获取原始图片size
CGFloat originalWidth = originalSize.width;//宽
CGFloat originalHeight = originalSize.height;//高
if ((originalWidth <= size.width) && (originalHeight <= size.height)) {
newImage = image;//宽和高同时小于要压缩的尺寸时返回原尺寸
}
else{
//新图片的宽和高
CGFloat scale = (float)size.width/originalWidth < (float)size.height/originalHeight ? (float)size.width/originalWidth : (float)size.height/originalHeight;
CGSize newImageSize = CGSizeMake(originalWidth*scale , originalHeight*scale );
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImageSize.width , newImageSize.height ), NO, 0);
[image drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if (newImage == nil) {
NSLog(@"image ");
}
UIGraphicsEndImageContext();
}
return newImage;
}