iOS图片压缩
/**
* 调整图片尺寸和大小
*
* @param source_Image 原始图片
*
* @param maxSize 新图片最大存储大小,单位为KB
*
* @return 新图片imageData
*/
- (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
{
//先调整分辨率
CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
CGFloat tempHeight = newSize.height / 1024;
CGFloat tempWidth = newSize.width / 1024;
if (tempWidth > 1.0 && tempWidth > tempHeight) {
newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
}
else if (tempHeight > 1.0 && tempWidth < tempHeight){
newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
}
UIGraphicsBeginImageContext(newSize);
[source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//调整大小
NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
NSUInteger sizeOrigin = [imageData length];
NSUInteger sizeOriginKB = sizeOrigin / 1024;
if (sizeOriginKB > maxSize) {
NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.50);
return finallImageData;
}
return imageData;
}