Linux 将 UIImage 保存到文件需要花费大量时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3676976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:06:21  来源:igfitidea点击:

saving UIImage to file cost a lot of time

iphonecocoa-touchuiimage

提问by disorderdev

I'm trying to load pictures in iPhone Photos app, then save the selected pictures in to my app's folder, using ALAssetsLibrary, and have two issues: 1. the image file saved to disk is much bigger then original files in Photos app, for example, a picture is 2.8MB, but after saved to my app's folder, it's 6.4MB, following is the code:

我正在尝试在 iPhone 照片应用程序中加载图片,然后使用 ALAssetsLibrary 将所选图片保存到我的应用程序文件夹中,并且有两个问题:1. 保存到磁盘的图像文件比照片应用程序中的原始文件大得多,对于例如,一张图片是2.8MB,但保存到我的应用程序文件夹后,它是6.4MB,以下是代码:

        CGImageRef cgImage = [localPhoto thumbnail];

        NSString *path = @"/documents/test/1.jpeg";//the path is just an example
        BOOL succ;
        UIImage *image1 = [UIImage imageWithCGImage:cgImage];

        NSData *data1 = UIImageJPEGRepresentation(image1, 1.0);
        succ = [data1 writeToFile:path atomically:YES];
  1. the above code(saving 6.4MB image to file) costs about 1.6seconds, is it normal? is there anyway to make it faster?
  1. 上面的代码(将 6.4MB 图像保存到文件)大约需要 1.6 秒,这正常吗?有没有办法让它更快?

采纳答案by Anil Sivadas

Try with PNG representation of the image.

尝试使用图像的 PNG 表示。

NSData *data1 = UIImagePNGRepresentation(image1);

or else reduce the image quality or JPG.

否则降低图像质量或 JPG。

NSData *data1 = UIImageJPEGRepresentation(image1, 0.5);