将UIIMAGE转换为数据,然后返回UIIMAGE在字节(size)中有所不同

发布于 2025-01-21 13:59:31 字数 846 浏览 3 评论 0原文

我遇到了一个问题,即将uiimage转换为data,然后回到uiimage使最后一个变为更大的[字节]。

示例(将任何大于3MB的照片添加到您的设备中):

guard let image = UIImage(named: "image_larger_than_3MB"),
      let data = image.jpegData(compressionQuality: 1)
else { print("failure"); return }
print(data.count)

guard let imageBack = UIImage(data: data),
      let dataBack = imageBack.jpegData(compressionQuality: 1)
else { print("failure 2"); return }
print(dataBack.count)

在我的情况下,我会收到以下控制台输出:

5304493 // = 5,06 MB // size before conversion
5575422 // = 5,32 MB // size after conversion

因此,初始图像的大小为5,06 MB。然后,我以最大质量将其转换为数据。然后,我再次将其转换为图像,并且大小更大,因为它是5,32 MB。问题随图像的大小而上升。

我了解问题可能与同时使用的压缩有关。无论如何,令我惊讶的是,大小更大而不是较小。

  1. 使用.jpeg Compression ..?
  2. 为什么尺寸增加(我知道它可以减少)?

I faced a problem, that converting the UIImage to Data and back to UIImage causes the last one to be larger [bytes].

Example (add to your device any photo which is larger than 3MB):

guard let image = UIImage(named: "image_larger_than_3MB"),
      let data = image.jpegData(compressionQuality: 1)
else { print("failure"); return }
print(data.count)

guard let imageBack = UIImage(data: data),
      let dataBack = imageBack.jpegData(compressionQuality: 1)
else { print("failure 2"); return }
print(dataBack.count)

In my case I receive the following console output:

5304493 // = 5,06 MB // size before conversion
5575422 // = 5,32 MB // size after conversion

So the initial image was of size 5,06 MB. Then I converted it to data with max quality. Then I convert it once again to image, and the size is larger, because it's 5,32 MB. The issue rises with the size of the image.

I understand that the issue is probably connected with compression used in the meantime. Anyway, it surprises me that the size is larger instead of smaller.

  1. Is there any way to maintain the second jpg size same to the initial jpg image when using .jpeg compression..?
  2. Why the size increased (I understand it could decrease)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文