ios压缩图片
/**
* 压缩图片到指定文件大小
*
* @param image 目标图片
* @param size 目标大小(最大值)
*
* @return 返回的图片文件
*/
- (NSData *)compressOriginalImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)size{
NSData * data = UIImageJPEGRepresentation(image, 1.0);
CGFloat dataKBytes = data.length/1000.0;
CGFloat maxQuality = 0.9f;
CGFloat lastData = dataKBytes;
while (dataKBytes > size && maxQuality > 0.01f) {
maxQuality = maxQuality - 0.01f;
data = UIImageJPEGRepresentation(image, maxQuality);
dataKBytes = data.length / 1000.0;
if (lastData == dataKBytes) {
break;
}else{
lastData = dataKBytes;
}
}
return data;
}
///原图压缩
-(NSData *)compressOriginalImage:(UIImage *)image
withRatio:(float)ratio
{
int largePicH = (int)image.size.height/ratio;
int largePicW = (int)image.size.width/ratio;
CGSize newSize = CGSizeMake(largePicW, largePicH);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//调整大小
NSData *imageData;
imageData = UIImageJPEGRepresentation(newImage, 0.5);
return imageData;
}
///获取压缩倍数
- (float)resetSizeOfImageData:(UIImage *)img
withLargePicH:(int)largePicH
withLargePicW:(int)largePicW
{
float inSampleSize = 1.0f;
int height = img.size.height;
int width = img.size.width;
if ( height > largePicH || width > largePicW) {
//swap value, make width is bigger than height
if(height > width){
int swapValue = height;
height = width;
width = swapValue;
}
// Calculate ratios of height and width to requested height and
// width
float heightRatio = (float)height/largePicH;
float widthRatio = (float)width/largePicW ;
inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
///缩略图压缩
-(NSData *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == 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 if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContextWithOptions(size, NO, 3.0);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//调整大小
NSData *imageData;
imageData = UIImageJPEGRepresentation(newImage,0.3);
return imageData;
}