像在whatsapp上一样在iPhone上压缩图像 [英] Compression of an image on an iPhone like on whatsapp

查看:130
本文介绍了像在whatsapp上一样在iPhone上压缩图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Swift编程语言中找到一个图像压缩算法,就像在WhatsApp信使中一样。在OS下编程语言Java下我发现很快,并且在iOS平台下有困难。 

以下是适用于Android的图像压缩算法:





  public   static 文件saveBitmapToFile(文件文件){
尝试 {
// 缩小图像的BitmapFactory选项
BitmapFactory .Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 6 ;
// 缩小图像的因素

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

// 我们想要扩展到的新尺寸
final int REQUIRED_SIZE = 75;

// 找到正确的比例值。应该是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();

// 这里我覆盖原始图像文件
file .createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);

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

return 文件;
} catch (例外e){
return null;
}
}





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





什么我试过了:



我研究了很多文章,试图使用不同的算法(遗憾的是我无法发布它们,因为我不记得网站),但我找不到必要的解决方案。

解决方案

Quote:

此算法将图像压缩为100 KB。



否,此代码不会将图像压缩为给定大小,它会缩小图片。缩小尺寸只是次要影响。

引用:

我正在尝试查找图像压缩算法



您不是在寻找压缩算法jpeg图像已被压缩。

您正在寻找的是一个允许您调整图像大小的库。


I'm trying to find an image compression algorithm in the Swift programming language, as in the WhatsApp messenger. In the programming language Java under OS Android I found quickly, and under the platform iOS have difficulties.

Here is the image compression algorithm for 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;
    }
}



This algorithm compresses the image to 100 KB. If you can tell me how to duplicate this code into the Swift programming language, I'll be very grateful.



What I have tried:

I have studied many articles, tried to use different algorithms (unfortunately I can not publish them, because I do not remember the sites), but I did not find the necessary solution.

解决方案

Quote:

This algorithm compresses the image to 100 KB.


No, this code do not compress the image to given size, it downscale the image. The reduced size is only a secondary effect.

Quote:

I'm trying to find an image compression algorithm


You are not looking for a compression algorithm jpeg images are already compressed.
What you are looking for is a library that will allow you to resize the images.


这篇关于像在whatsapp上一样在iPhone上压缩图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆