图片压缩

对于“压”功能,我们可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation方法实现

第一个参数是图片对象本身,第二个参数是压的系数,其值范围为(0-1);

NSData*imgData =UIImageJPEGRepresentation(image,0.5);

图片“缩”处理:

主要通过 [image drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)]; 实现。

/**

*  1, 按图片最大边成比例缩放图片

*

*  @param image  图片

*  @param maxSize 图片的较长那一边目标缩到的(宽度/高度)

*

*  @return        等比缩放后的图片

*/- (UIImage*)scaleImage:(UIImage*)image maxSize:(CGFloat)maxSize {NSData*data =UIImageJPEGRepresentation(image,1.0);if(data.length <200*1024){//0.25M-0.5M(当图片小于此范围不压缩)returnimage;    }CGFloatimageWidth = image.size.width;CGFloatimageHeight = image.size.height;CGFloattargetWidth = imageWidth;CGFloattargetHeight = imageHeight;CGFloatimageMaxSize = MAX(imageWidth, imageHeight);if(imageMaxSize > maxSize) {CGFloatscale =0;if(imageWidth >= imageHeight) {// 宽长scale = maxSize / imageWidth;            targetWidth = maxSize;            targetHeight = imageHeight * scale;        }else{// 高长scale = maxSize / imageHeight;            targetHeight = maxSize;            targetWidth = imageWidth * scale;        }UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));        [image drawInRect:CGRectMake(0,0, targetWidth, targetHeight)];UIImage*scaledImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnscaledImage;    }returnimage;}

/**

*  2, 图片支持等比缩放

*

*  @param image  图片

*  @param maxSize 缩放比例(通常0~1之间)

*

*  @return        等比缩放后的图片

*/- (UIImage*)scaleImage:(UIImage*)image toScale:(float)scaleSize {UIGraphicsBeginImageContext(CGSizeMake(image.size.width *scaleSize, image.size.height * scaleSize));    [image drawInRect:CGRectMake(0,0, image.size.width * scaleSize, image.size.height * scaleSize)];UIImage*scaledImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnscaledImage;}

/**

*  3, 等比缩放成自定长宽的图片

*

*  @param image      源图片

*  @param targetSize 自定义目标图片的size

*

*  @return 处理后图片

*/- (UIImage*)scaleImage:(UIImage*)image toSize:(CGSize)targetSize {UIGraphicsBeginImageContext(CGSizeMake(targetSize.width, targetSize.height));    [image drawInRect:CGRectMake(0,0, targetSize.width, targetSize.height)];UIImage*targetSizeImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returntargetSizeImage;}//根据图片附获取图片大小(多少M)方法- (NSData*)imageData:(UIImage*)image {NSData*data =UIImageJPEGRepresentation(image,1.0);if(data.length >100*1024) {if(data.length >1024*1024) {//1M以及以上data =UIImageJPEGRepresentation(image,0.1);        }elseif(data.length >512*1024) {//0.5M-1Mdata =UIImageJPEGRepresentation(image,0.5);        }elseif(data.length >200*1024) {//0.25M-0.5Mdata =UIImageJPEGRepresentation(image,0.9);        }    }returndata;}

原图大小为24.9M, 4288 * 2848像素;经一次UIImageJPEGRepresentation“压“处理

UIImage*tmpImage = [UIImageimageWithContentsOfFile:photo.photoPath];NSData*imageData =UIImageJPEGRepresentation(tmpImage,0.5);

图片大小为836.9K, 4288 * 2848像素;

经一次UIImagePNGRepresentation“压“处理

UIImage*tmpImage = [UIImageimageWithContentsOfFile:photo.photoPath];NSData*imageData =UIImagePNGRepresentation(tmpImage);

图片大小为24.9M, 4288 * 2848像素;几乎没变

经一次“缩“处理(笔者给予图片最大的宽度限定为640像素)

UIImage*tmpImage = [UIImageimageWithContentsOfFile:photo.photoPath];tmpImage = [selfscaleImage:tmpImage maxSize:640];

图片大小为795.9K, 640 * 426像素;

经一次“压缩”处理:

UIImage*tmpImage = [UIImageimageWithContentsOfFile:photo.photoPath];tmpImage = [selfscaleImage:tmpImage maxSize:640];NSData*imageData =UIImageJPEGRepresentation(tmpImage,0.5);

图片大小为698K, 640 * 426像素;大家可以根据实际调整不同参数以达到自己的项目要求。

你可能感兴趣的:(图片压缩)