在項(xiàng)目中經(jīng)常遇到要上傳圖片胯究,如果直接上傳,那么會(huì)上傳比較大的圖片躁绸,導(dǎo)致費(fèi)流量裕循,刷新時(shí)加載圖片時(shí)間過長,手機(jī)內(nèi)存占用率高等問題净刮。
一剥哑、先來介紹下概念:
圖片的壓縮其實(shí)是倆概念,
1淹父、是 “壓” 文件體積變小株婴,但是像素?cái)?shù)不變,長寬尺寸不變暑认,那么質(zhì)量可能下降困介,
2、是 “縮” 文件的尺寸變小蘸际,也就是像素?cái)?shù)減少座哩。長寬尺寸變小,文件體積同樣會(huì)減小捡鱼。
二八回、解決方法(以上傳頭像為例),先縮再壓:
2.1 矯正圖片方向(照片是有方向的驾诈,避免出現(xiàn)“倒立”的情況)
#pragma mark -矯正手機(jī)相冊(cè)里圖像的方向
- (UIImage*)fixOrientation:(UIImage*)aImage {
// No-op if the orientation is already correct
if(aImage.imageOrientation==UIImageOrientationUp)
returnaImage;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransformtransform =CGAffineTransformIdentity;
switch(aImage.imageOrientation) {
caseUIImageOrientationDown:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform =CGAffineTransformRotate(transform,M_PI);
break;
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformRotate(transform,M_PI_2);
break;
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform,0, aImage.size.height);
transform =CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch(aImage.imageOrientation) {
caseUIImageOrientationUpMirrored:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.height,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRefctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage),0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch(aImage.imageOrientation) {
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
CGImageRef cgimg =CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImageimageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
2.2 拿到上面矯正過的圖片缠诅,縮小圖片尺寸,調(diào)用下面方法傳入newSize乍迄,如(200管引,200):
//縮小圖片尺寸
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
2.3 將2.2的圖片再壓,這個(gè)方法可以重復(fù)壓
//調(diào)整大小
NSData *imageData =UIImageJPEGRepresentation(newImage,rate);
NSUIntegersizeOrigin = [image Datalength];//多少KB
NSUIntegersizeOriginKB = sizeOrigin /1024;//多少KB
2.4 上傳頭像
調(diào)用后臺(tái)接口闯两,把imageData二進(jìn)制數(shù)據(jù)上傳即可
總結(jié):對(duì)圖片壓縮處理時(shí)褥伴,在保證圖片清晰度變化不大時(shí)谅将,減小圖片文件大小。方法2.2中的newSize 和 2.3中的rate要以實(shí)際效果來設(shè)置重慢,我在自己項(xiàng)目中上傳的頭像最終尺寸是200*200像素饥臂,大小為4KB左右。