上傳前把圖片縮小,能省下可觀的流量與伺服器空間。以下三種做法差別在於是否保持長寬比、以及是否裁切。
取寬高兩個方向的縮放比例中較小者,確保圖片完整塞進目標框內且不變形。
- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize {
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
if (width <= newSize.width && height <= newSize.height) return image; // 已夠小
if (width == 0 || height == 0) return image;
CGFloat widthFactor = newSize.width / width;
CGFloat heightFactor = newSize.height / height;
CGFloat scaleFactor = (widthFactor < heightFactor ? widthFactor : heightFactor);
CGFloat scaledWidth = width * scaleFactor;
CGFloat scaledHeight = height * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(scaledWidth, scaledHeight));
[image drawInRect:CGRectMake(0, 0, scaledWidth, scaledHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
把縮放與「縮放並裁切」寫成 UIImage 的分類,呼叫端更乾淨。
// UIImageExt.h #import <Foundation/Foundation.h> @interface UIImage (UIImageExt) - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; @end
// UIImageExt.m
#import "UIImageExt.h"
@implementation UIImage (UIImageExt)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size {
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
// 取較大比例填滿目標,再居中裁切
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
scaleFactor = (widthFactor > heightFactor) ? widthFactor : 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;
}
UIGraphicsBeginImageContext(targetSize); // 超出部分會被裁掉
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if (newImage == nil) NSLog(@"could not scale image");
UIGraphicsEndImageContext();
return newImage;
}
@end
實務上最常用的一種:只給目標寬度,高度依原始比例自動算出。適合清一色要轉成固定寬度的縮圖情境。
-(UIImage *)imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth {
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = defineWidth;
CGFloat targetHeight = (targetWidth / width) * height;
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
[sourceImage drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
要補充的是,這三種處理的都是「尺寸」,若要壓縮「檔案體積」,還需要搭配 UIImageJPEGRepresentation(image, quality),用 0.0~1.0 的品質參數調整。實務上常見組合是:先限制最長邊(例如 1280px),再以 0.7 左右的品質轉 JPEG,多數情況下視覺差異不明顯而檔案小很多。若圖片需要透明背景,則必須用 PNG,此時只能靠縮小尺寸來減量。