#define KCompressibilityFactor 1280.00
@implementationWeChatJPEGImager
#pragma?mark?-?壓縮一張圖片?最大寬高1280?類似于微信算法
-?(UIImage*)getJPEGImagerImg:(UIImage*)image{
CGFloat?oldImg_WID?=?image.size.width;
CGFloat?oldImg_HEI?=?image.size.height;
//CGFloat?aspectRatio?=?oldImg_WID/oldImg_HEI;//寬高比
if(oldImg_WID?>?KCompressibilityFactor?||?oldImg_HEI?>?KCompressibilityFactor){
//超過設(shè)置的最大寬度?先判斷那個邊最長
if(oldImg_WID?>?oldImg_HEI){
//寬度大于高度
oldImg_HEI?=?(KCompressibilityFactor*?oldImg_HEI)/oldImg_WID;
oldImg_WID?=?KCompressibilityFactor;
}else{
oldImg_WID?=?(KCompressibilityFactor*?oldImg_WID)/oldImg_HEI;
oldImg_HEI?=?KCompressibilityFactor;
}
}
UIImage*newImg?=?[selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID,?oldImg_HEI)];
NSData*dJpeg?=nil;
if(UIImagePNGRepresentation(newImg)==nil)?{
dJpeg?=?UIImageJPEGRepresentation(newImg,0.5);
}else{
dJpeg?=?UIImagePNGRepresentation(newImg);
}
return[UIImageimageWithData:dJpeg];
}
#pragma?mark?-?壓縮多張圖片?最大寬高1280?類似于微信算法
-?(NSArray*)getJPEGImagerImgArr:(NSArray*)imageArr{
NSMutableArray*newImgArr?=?[NSMutableArraynew];
for(inti?=0;?i
UIImage*newImg?=?[selfgetJPEGImagerImg:imageArr[i]];
[newImgArraddObject:newImg];
}
returnnewImgArr;
}
#pragma?mark?-?壓縮一張圖片?自定義最大寬高
-?(UIImage*)getJPEGImagerImg:(UIImage*)imagecompressibilityFactor:(CGFloat)compressibilityFactor{
CGFloat?oldImg_WID?=?image.size.width;
CGFloat?oldImg_HEI?=?image.size.height;
//CGFloat?aspectRatio?=?oldImg_WID/oldImg_HEI;//寬高比
if(oldImg_WID?>?compressibilityFactor?||?oldImg_HEI?>?compressibilityFactor){
//超過設(shè)置的最大寬度?先判斷那個邊最長
if(oldImg_WID?>?oldImg_HEI){
//寬度大于高度
oldImg_HEI?=?(compressibilityFactor*?oldImg_HEI)/oldImg_WID;
oldImg_WID?=?compressibilityFactor;
}else{
oldImg_WID?=?(compressibilityFactor*?oldImg_WID)/oldImg_HEI;
oldImg_HEI?=?compressibilityFactor;
}
}
UIImage*newImg?=?[selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID,?oldImg_HEI)];
NSData*dJpeg?=nil;
if(UIImagePNGRepresentation(newImg)==nil)?{
dJpeg?=?UIImageJPEGRepresentation(newImg,0.5);
}else{
dJpeg?=?UIImagePNGRepresentation(newImg);
}
return[UIImageimageWithData:dJpeg];
}
#pragma?mark?-?壓縮多張圖片?自定義最大寬高
-?(NSArray*)getJPEGImagerImgArr:(NSArray*)imageArrcompressibilityFactor:(CGFloat)compressibilityFactor{
NSMutableArray*newImgArr?=?[NSMutableArraynew];
for(inti?=0;?i
UIImage*newImg?=?[selfgetJPEGImagerImg:imageArr[i]compressibilityFactor:compressibilityFactor];
[newImgArraddObject:newImg];
}
returnnewImgArr;
}
#pragma?mark?-?根據(jù)寬高壓縮圖片
-?(UIImage*)imageWithImage:(UIImage*)imagescaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage*?newImage?=?UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnnewImage;
}