UIImage的jpg或png - 效率更高?

时间:2022-06-29 04:18:35

I am grabbing an image from the camera roll and then using it for a while as well as save it to disk as a PNG on the iPhone. I am getting the odd crash, presumably due to out of memory.

我从相机胶卷中抓取一张图像,然后使用它一段时间,并将其作为iPhone上的PNG保存到磁盘上。我得到了奇怪的崩溃,大概是因为内存不足。

Does it make a difference if I save it as PNG or JPG (assuming I choose note to degrade the quality in the JPG case)? Specifically:

如果我将其保存为PNG或JPG(假设我选择注意降低JPG案例的质量)会有所不同吗?特别:

  • is more memory then used by the UIImage after I reload it off of disk if I saved it as a PNG?
  • 如果我把它保存为PNG,那么当我将其从磁盘上重新加载后,UIImage会使用更多的内存吗?

  • is it possible the act of saving as PNG uses up more memory transiently during the saving process?
  • 在保存过程中,作为PNG保存的行为可能会暂时消耗更多内存吗?

I had been assuming the UIImage was a format neutral representation and it shouldn't matter, but I thought I should verify.

我一直认为UIImage是一个格式中立的表示,它应该没关系,但我想我应该验证。

7 个解决方案

#1


10  

I have an application on the store that needs to save intermediate versions of an image as it's being edited. In the original version, I used PNG format for saving, to avoid quality loss from loading and saving JPEG multiple times.

我在商店有一个应用程序,需要在编辑时保存图像的中间版本。在原始版本中,我使用PNG格式进行保存,以避免加载质量和多次保存JPEG。

Sometime around the 2.2 software release, Apple introduced a change into the PNG writing code, such that it takes many times longer to save PNG data from some images. I ended up having to change to saving in JPEG format, because my application was timing out when trying to save images on exit.

在软件版本2.2的某个时候,Apple引入了对PNG编写代码的更改,因此从某些图像中保存PNG数据需要花费很多倍的时间。我最终不得不改为以JPEG格式保存,因为我的应用程序在尝试退出时保存图像时超时。

Also, you'll run into issues because saving in PNG format doesn't preserve the "orientation" information in the UIImage, so a picture taken in Portrait orientation with the built-in camera will appear rotated after you save and reload it.

此外,您将遇到问题,因为以PNG格式保存不会保留UIImage中的“方向”信息,因此在保存并重新加载后,使用内置相机以纵向方向拍摄的照片将显示为旋转。

#2


24  


I am getting the odd crash, presumably due to out of memory

我得到了奇怪的崩溃,大概是因为内存不足


Then STOP WHAT YOU ARE DOING RIGHT NOW and first figure out if that's actually the cause of the crash. Otherwise there's a very good chance that you're chasing the wrong problem here, fixing a memory problem that doesn't exist while ignoring the real cause of the crash. If you want to fix a crash, start by figuring out what caused the crash. Following up on what's "presumably" the problem is a recipe for wasted time and effort.

然后停止你现在正在做什么,并首先弄清楚这是否真的是崩溃的原因。否则,你很有可能在这里追逐错误的问题,修复一个不存在的内存问题而忽略崩溃的真正原因。如果你想修复崩溃,首先要弄清楚导致崩溃的原因。跟进“可能”的问题是浪费时间和精力的方法。

#3


5  

It depends on what type of images you're dealing with. If you're dealing with photographic images, JPEGs will almost always be smaller than PNGs, with no discernable loss of detail as can be seen by the human eye.

这取决于您正在处理的图像类型。如果你正在处理摄影图像,JPEG几乎总是小于PNG,没有人眼可以看到的明显的细节损失。

Conversely, if you're dealing with highly non-photographic images such as GUI elements or images with large blocks of solid colors, then PNGs and JPEGs will be comparable in size, but the PNG will save losslessly whereas the JPEG will be lossy and have very visible artifacts. If you have a really simple image (very large blocks of constant colors, e.g.), then a PNG will very likely be much smaller than a JPEG, and again will not have any compression artifacts.

相反,如果你正在处理高度非摄影图像,如GUI元素或具有大块纯色的图像,那么PNG和JPEG的大小将相当,但PNG将无损保存,而JPEG将是有损的并且具有非常明显的文物。如果你有一个非常简单的图像(例如非常大的恒定颜色块),那么PNG很可能比JPEG小得多,并且再次不会有任何压缩伪像。

The act of saving an image as a PNG or JPEG should not take up very much transient memory. When an image is in memory, it is typically stored uncompressed in memory so that it can be drawn to the screen very quickly, as opposed to having to decompress it every time you want to render it. Compared to the size of the uncompressed image, the amount of extra temporary storage you need to compress it is very small. If you can fit the uncompressed image in memory, you don't have to worry about the memory used while compressing it.

将图像保存为PNG或JPEG的行为不应占用非常短暂的内存。当图像在内存中时,它通常以未压缩的形式存储在内存中,以便可以非常快速地将其绘制到屏幕上,而不是每次要渲染它时都必须将其解压缩。与未压缩图像的大小相比,压缩它所需的额外临时存储量非常小。如果您可以将未压缩的图像放入内存中,则无需担心压缩时使用的内存。

And of course, once you write the image to the file system in non-volatile storage and free the in-memory image, it really doesn't matter how big the compressed image is, because it doesn't take up main memory any more. The size of the compressed image only affects how much flash storage it's using, which can be an issue, but it does not affect high likely your app is to run out of memory.

当然,一旦将图像写入非易失性存储中的文件系统并释放内存中的图像,压缩图像的大小并不重要,因为它不再占用主存储器。压缩图像的大小仅影响它使用的闪存存储量,这可能是一个问题,但它不会影响您的应用程序内存不足的可能性。

#4


1  

Your crashes may be from a known memory leak in the UIImagePickerController.

您的崩溃可能来自UIImagePickerController中的已知内存泄漏。

This should help you fix that.

这可以帮助您解决这个问题。

#5


0  

I don't have any hard data, but I'd assume that PNGs are preferable because Apple seems to use PNGs virtually everywhere in iPhone OS.

我没有任何硬数据,但我认为PNG更受欢迎,因为Apple似乎在iPhone OS中几乎无处不在使用PNG。

However, if you've already got the code set up for writing PNGs, it shouldn't be too hard to change it to write JPEGs, should it? Just try both methods and see which works better.

但是,如果您已经为编写PNG设置了代码,那么将其更改为编写JPEG应该不会太难,是吗?试试两种方法,看看哪种方法效果更好。

#6


0  

Use PNG wherever possible. As part of the compilation XCode runs all PNG files through a utility (pngcrush) to compress and optimize them.

尽可能使用PNG。作为编译的一部分,XCode通过实用程序(pngcrush)运行所有PNG文件以压缩和优化它们。

#7


0  

  1. is more memory then used by the UIImage after I reload it off of disk if I saved it as a PNG? => No, it's the same memory size if you import from 2 images that have same resolution and same number of channels. (such as RGBA)
  2. 如果我把它保存为PNG,那么当我将其从磁盘上重新加载后,UIImage会使用更多的内存吗? =>不,如果您从具有相同分辨率和相同通道数的2个图像导入,则它的内存大小相同。 (如RGBA)

  3. is it possible the act of saving as PNG uses up more memory transiently during the saving process? => No, it only effect your disk memory.
  4. 在保存过程中,作为PNG保存的行为可能会暂时消耗更多内存吗? =>不,它只影响你的磁盘内存。

#1


10  

I have an application on the store that needs to save intermediate versions of an image as it's being edited. In the original version, I used PNG format for saving, to avoid quality loss from loading and saving JPEG multiple times.

我在商店有一个应用程序,需要在编辑时保存图像的中间版本。在原始版本中,我使用PNG格式进行保存,以避免加载质量和多次保存JPEG。

Sometime around the 2.2 software release, Apple introduced a change into the PNG writing code, such that it takes many times longer to save PNG data from some images. I ended up having to change to saving in JPEG format, because my application was timing out when trying to save images on exit.

在软件版本2.2的某个时候,Apple引入了对PNG编写代码的更改,因此从某些图像中保存PNG数据需要花费很多倍的时间。我最终不得不改为以JPEG格式保存,因为我的应用程序在尝试退出时保存图像时超时。

Also, you'll run into issues because saving in PNG format doesn't preserve the "orientation" information in the UIImage, so a picture taken in Portrait orientation with the built-in camera will appear rotated after you save and reload it.

此外,您将遇到问题,因为以PNG格式保存不会保留UIImage中的“方向”信息,因此在保存并重新加载后,使用内置相机以纵向方向拍摄的照片将显示为旋转。

#2


24  


I am getting the odd crash, presumably due to out of memory

我得到了奇怪的崩溃,大概是因为内存不足


Then STOP WHAT YOU ARE DOING RIGHT NOW and first figure out if that's actually the cause of the crash. Otherwise there's a very good chance that you're chasing the wrong problem here, fixing a memory problem that doesn't exist while ignoring the real cause of the crash. If you want to fix a crash, start by figuring out what caused the crash. Following up on what's "presumably" the problem is a recipe for wasted time and effort.

然后停止你现在正在做什么,并首先弄清楚这是否真的是崩溃的原因。否则,你很有可能在这里追逐错误的问题,修复一个不存在的内存问题而忽略崩溃的真正原因。如果你想修复崩溃,首先要弄清楚导致崩溃的原因。跟进“可能”的问题是浪费时间和精力的方法。

#3


5  

It depends on what type of images you're dealing with. If you're dealing with photographic images, JPEGs will almost always be smaller than PNGs, with no discernable loss of detail as can be seen by the human eye.

这取决于您正在处理的图像类型。如果你正在处理摄影图像,JPEG几乎总是小于PNG,没有人眼可以看到的明显的细节损失。

Conversely, if you're dealing with highly non-photographic images such as GUI elements or images with large blocks of solid colors, then PNGs and JPEGs will be comparable in size, but the PNG will save losslessly whereas the JPEG will be lossy and have very visible artifacts. If you have a really simple image (very large blocks of constant colors, e.g.), then a PNG will very likely be much smaller than a JPEG, and again will not have any compression artifacts.

相反,如果你正在处理高度非摄影图像,如GUI元素或具有大块纯色的图像,那么PNG和JPEG的大小将相当,但PNG将无损保存,而JPEG将是有损的并且具有非常明显的文物。如果你有一个非常简单的图像(例如非常大的恒定颜色块),那么PNG很可能比JPEG小得多,并且再次不会有任何压缩伪像。

The act of saving an image as a PNG or JPEG should not take up very much transient memory. When an image is in memory, it is typically stored uncompressed in memory so that it can be drawn to the screen very quickly, as opposed to having to decompress it every time you want to render it. Compared to the size of the uncompressed image, the amount of extra temporary storage you need to compress it is very small. If you can fit the uncompressed image in memory, you don't have to worry about the memory used while compressing it.

将图像保存为PNG或JPEG的行为不应占用非常短暂的内存。当图像在内存中时,它通常以未压缩的形式存储在内存中,以便可以非常快速地将其绘制到屏幕上,而不是每次要渲染它时都必须将其解压缩。与未压缩图像的大小相比,压缩它所需的额外临时存储量非常小。如果您可以将未压缩的图像放入内存中,则无需担心压缩时使用的内存。

And of course, once you write the image to the file system in non-volatile storage and free the in-memory image, it really doesn't matter how big the compressed image is, because it doesn't take up main memory any more. The size of the compressed image only affects how much flash storage it's using, which can be an issue, but it does not affect high likely your app is to run out of memory.

当然,一旦将图像写入非易失性存储中的文件系统并释放内存中的图像,压缩图像的大小并不重要,因为它不再占用主存储器。压缩图像的大小仅影响它使用的闪存存储量,这可能是一个问题,但它不会影响您的应用程序内存不足的可能性。

#4


1  

Your crashes may be from a known memory leak in the UIImagePickerController.

您的崩溃可能来自UIImagePickerController中的已知内存泄漏。

This should help you fix that.

这可以帮助您解决这个问题。

#5


0  

I don't have any hard data, but I'd assume that PNGs are preferable because Apple seems to use PNGs virtually everywhere in iPhone OS.

我没有任何硬数据,但我认为PNG更受欢迎,因为Apple似乎在iPhone OS中几乎无处不在使用PNG。

However, if you've already got the code set up for writing PNGs, it shouldn't be too hard to change it to write JPEGs, should it? Just try both methods and see which works better.

但是,如果您已经为编写PNG设置了代码,那么将其更改为编写JPEG应该不会太难,是吗?试试两种方法,看看哪种方法效果更好。

#6


0  

Use PNG wherever possible. As part of the compilation XCode runs all PNG files through a utility (pngcrush) to compress and optimize them.

尽可能使用PNG。作为编译的一部分,XCode通过实用程序(pngcrush)运行所有PNG文件以压缩和优化它们。

#7


0  

  1. is more memory then used by the UIImage after I reload it off of disk if I saved it as a PNG? => No, it's the same memory size if you import from 2 images that have same resolution and same number of channels. (such as RGBA)
  2. 如果我把它保存为PNG,那么当我将其从磁盘上重新加载后,UIImage会使用更多的内存吗? =>不,如果您从具有相同分辨率和相同通道数的2个图像导入,则它的内存大小相同。 (如RGBA)

  3. is it possible the act of saving as PNG uses up more memory transiently during the saving process? => No, it only effect your disk memory.
  4. 在保存过程中,作为PNG保存的行为可能会暂时消耗更多内存吗? =>不,它只影响你的磁盘内存。