在iPhone上像WhatsApp上的图像压缩

时间:2018-08-20 16:03:25

标签: ios swift compression

我正试图在Swift编程语言中找到一种图像压缩算法,就像WhatsApp Messenger一样。我很快找到了Android OS和Java平台下的Java语言。

这是Android的图像压缩算法:

public static File saveBitmapToFile(File file){
    try {
        // BitmapFactory options to downsize the image
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;
        // factor of downsizing the image

        FileInputStream inputStream = new FileInputStream(file);
        //Bitmap selectedBitmap = null;
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        // The new size we want to scale to
        final int REQUIRED_SIZE=75;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = new FileInputStream(file);

        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();

        // here i override the original image file
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);

        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 70 , outputStream);

        return file;
    } catch (Exception e) {
        return null;
    }
}

此算法将图像压缩到100 KB。如果您能告诉我如何将此代码复制到Swift编程语言中,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以使用UIImageJPEGRepresentation方法压缩UIImage。以下示例显示了0.4的压缩率。

let imageData = UIImageJPEGRepresentation(image, 0.4)! //image is your original image
let compressedImage = UIImage(data: imageData)

答案 1 :(得分:0)

此功能应执行以下操作:

func compressImage(_ image: UIImage?) -> UIImage? {
var actualHeight = Float(image?.size.height ?? 0.0)
var actualWidth = Float(image?.size.width ?? 0.0)
let maxHeight: Float = 600.0
let maxWidth: Float = 800.0
var imgRatio: Float = actualWidth / actualHeight
let maxRatio: Float = maxWidth / maxHeight
let compressionQuality: Float = 0.5 //50 percent compression

if actualHeight > maxHeight || actualWidth > maxWidth {
    if imgRatio < maxRatio {
        //adjust width according to maxHeight
        imgRatio = maxHeight / actualHeight
        actualWidth = imgRatio * actualWidth
        actualHeight = maxHeight
    } else if imgRatio > maxRatio {
        //adjust height according to maxWidth
        imgRatio = maxWidth / actualWidth
        actualHeight = imgRatio * actualHeight
        actualWidth = maxWidth
    } else {
        actualHeight = maxHeight
        actualWidth = maxWidth
    }
}

    let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
    UIGraphicsBeginImageContext(rect.size)
    image?.draw(in: rect)
    let img: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality))
    UIGraphicsEndImageContext()

    if let imageData = imageData {
        return UIImage(data: imageData)
    }
    return nil
}

用法:

secondHighest.image = compressImage(image, quality: 0.0) //您总是可以添加这样的参数来控制质量

Highest.image = compressImage(image, quality: 0.6)//bottom

结果:

enter image description here