什么时候应该使用UIImageJPEGRepresentation和UIImagePNGRepresentation上传不同的图像格式到服务器? [英] When should I use UIImageJPEGRepresentation and UIImagePNGRepresentation for uploading different image formats to the server?

查看:563
本文介绍了什么时候应该使用UIImageJPEGRepresentation和UIImagePNGRepresentation上传不同的图像格式到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须发送不同格式的图像到服务器(它必须是所有可以通过 UIImage 类读取的文件格式)https://developer.apple.com/library/ios/#documentation/uikit/ reference / UIImage_Class / Reference / Reference.html

问题是:我不知道什么时候应该使用这些方法。当然很明显,对于 .png 图像,我需要使用 UIImagePNGRepresentation .jpg /.jpeg UIImageJPEGRepresentation 。但是其他格式( .tiff .gif 等)?

In my application I have to send images of different formats to the server (it must be all file formats that can be read by the UIImage class) https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html

And the problem is: I don't know when I should use each of this methods. Of course it's obvious that for .png images I need to use UIImagePNGRepresentation and for .jpg/.jpeg UIImageJPEGRepresentation. But what about other formats (.tiff,.gif , etc.)? There are only two methods for image manipulations and so many formats.

推荐答案

您说:


当然很明显,对于.png图像,我需要使用 UIImagePNGRepresentation 和.jpg / .jpeg UIImageJPEGRepresentation

Of course it's obvious that for .png images I need to use UIImagePNGRepresentation and for .jpg/.jpeg UIImageJPEGRepresentation.

不一定是这种情况。如果你有一些原始的数字资产,而不是创建一个 UIImage ,然后使用这两个函数之一创建 NSData ,您通常只需从原始资产加载 NSData ,然后绕过 UIImage code>。如果这样做,您不会冒任何转换为​​ UIImage 的数据丢失的风险,然后再次返回,可能会导致。

No, that's not necessarily the case. If you have some original "digital asset", rather than creating a UIImage and then using one of those two functions to create the NSData that you'll upload, you will often just load the NSData from the original asset and bypass the round-trip to a UIImage at all. If you do this, you don't risk any loss of data that converting to a UIImage, and then back again, can cause.

但有一些额外的考虑:


  1. 元数据:

  1. Meta data:

这些 UIImageXXXRepresentation 函数剥离其元数据的图像。有时,这是一件好事(例如,您不想上传您的孩子的照片或昂贵的小工具,包括GPS位置,恶意内容可以识别拍摄的地方)。在其他情况下,您不希望元数据被丢弃(例如原始镜头的日期,相机等)。

These UIImageXXXRepresentation functions strip the image of its meta data. Sometimes that's a good thing (e.g. you don't want to upload photos of your children or expensive gadgets the include the GPS locations where malcontents could identify where the shot was taken). In other cases, you don't want the meta data to be thrown away (e.g. date of the original shot, which camera, etc.).

您应该明确决定是否要删除元数据。如果没有,请不要通过 UIImage 往返旅行您的图片,而是使用原始资产。

You should make an explicit decision as to whether you want meta data stripped or not. If not, don't round-trip your image through a UIImage, but rather use the original asset.

图片质量损失和/或文件大小注意事项:

Image quality loss and/or file size considerations:

我对 UIImageJPEGRepresentation 它是有损压缩。因此,如果你使用 compressionQuality 值小于1.0,你可能会失去一些图像质量(适度的质量损失接近1.0,更显着的质量损失与 compressionQuality 值)。如果你使用1.0的 compressionQuality ,你可以减少JPEG图像质量的损失,但是结果 NSData

I'm particularly not crazy about UIImageJPEGRepresentation because it a lossy compression. Thus, if you use a compressionQuality value smaller than 1.0, you can lose some image quality (modest quality loss for values close to 1.0, more significant quality loss with lower compressionQuality values). And if you use a compressionQuality of 1.0, you mitigate much of the JPEG image quality loss, but the resulting NSData can often be bigger than the original asset (at least if the original was, itself, a compressed JPEG or PNG), resulting in slower uploads.

这是一个问题,您在上传过程中是否确定有某些图片质量损失和/或文件大小较大。

It's a question of whether you are ok with some image quality loss and/or larger file size during the upload process.

图片大小:

有时您不想上传完整分辨率的图片。例如,您可能正在使用一个Web服务,想要的图像不大于800px每边。或者,如果您要上传缩图,他们可能想要更小的(例如32像素x 32像素)。通过调整图像大小,您可以使上传更小,因此更快(虽然有明显的质量损失)。但如果您使用图片调整大小算法,则使用这些 UIImageXXXRepresentation 创建PNG或JPEG, code>的函数会很常见。

Sometimes you don't want to upload the full resolution image. For example, you might be using a web service that wants images no bigger than 800px per side. Or if you're uploading a thumbnail, they might want something even smaller (e.g. 32px x 32px). By resizing images, you can make the upload much smaller and thus much faster (though with obvious quality loss). But if you use an image resizing algorithm, then creating a PNG or JPEG using these UIImageXXXRepresentation functions would be quite common.

总之,如果我试图最小化数据/质量损失,如果是服务器接受的格式,我会上传原始资源,我会使用 UIImagePNGRepresentation (或 UIImageJPGRepresentation ,质量设置为1.0),如果原始资产不是服务器接受的格式。但是使用这些 UIImageXXXRepresentation 函数的选择是您的业务需求和服务器接受的问题。

In short, if I'm trying to minimize the data/quality loss, I would upload the original asset if it's in a format that the server accepts, and I'd use UIImagePNGRepresentation (or UIImageJPGRepresentation with quality setting of 1.0) if the original asset was not in a format accepted by the server. But the choice of using these UIImageXXXRepresentation functions is a question of your business requirements and what the server accepts.

这篇关于什么时候应该使用UIImageJPEGRepresentation和UIImagePNGRepresentation上传不同的图像格式到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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