您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 圖片壓縮的三種常見方案

編輯:iOS 相關教學

上傳頭像、分享照片時,若直接把相簿原圖丟上網路,流量與等待時間都很可觀。iOS 提供幾種常見的縮圖思路,以下整理三種實作方式,可依專案需求挑選。

方法一:等比縮放到指定尺寸

核心概念是取寬高比例中較小的縮放因子,確保長寬都不會超出目標範圍,再重繪到新的位圖環境:

- (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;
    CGSize targetSize = CGSizeMake(scaledWidth, scaledHeight);

    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0, 0, scaledWidth, scaledHeight)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

方法二:封裝成 UIImage 分類

把縮圖能力做成分類,呼叫端語意更直觀。頭檔如下:

#import <Foundation/Foundation.h>

@interface UIImage (UIImageExt)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end

實作檔:

#import "UIImageExt.h"

@implementation UIImage (UIImageExt)

// 單純縮放
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{
    // 建立 bitmap context 並設為當前繪圖環境
    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;
        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;
        }
    }

    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

方法三:只指定目標寬度(實務最常用)

很多時候我們只知道「寬度固定 300」,高度讓它等比縮放就好。這種寫法最精簡,也是筆者在專案中的首選:

- (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;
}

還有一條路:直接調 JPG 壓縮品質

上面三種都是「改尺寸」,若想「改品質」,可以直接用 UIImageJPEGRepresentation(image, quality),把 quality 從 0 到 1 之間調低,實務上常與縮圖搭配使用:先縮尺寸,再以 0.7 左右的品質編碼上傳,能兼顧清晰度與檔案大小。此外,若追求效能,也可改用 UIGraphicsImageRenderer 或 CGImageSourceCreateThumbnailAtIndex 直接解碼縮圖,避免把整張原圖攤在記憶體裡。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved