何时应该使用 UIImageJPEGRepresentation 和 UIImagePNGRepresentation 将不同的图像格式上传到服务器?

When should I use UIImageJPEGRepresentation and UIImagePNGRepresentation for uploading different image formats to the server?(何时应该使用 UIImageJPEGRepresentation 和 UIImagePNGRepresentation 将不同的图像格式上传到服务器?)

本文介绍了何时应该使用 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 的往返.如果您这样做,则不会冒任何数据丢失的风险,因为转换为 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.

不过,还有一些额外的注意事项:

There are some additional considerations, though:

  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,因为它是一种有损压缩一个>.因此,如果您使用小于 1.0 的 compressionQuality 值,您可能会损失一些图像质量(接近 1.0 的值会有适度的质量损失,而 compressionQuality 值越低则质量损失越大).如果您使用 1.0 的 compressionQuality,您可以减轻大部分 JPEG 图像质量损失,但生成的 NSData 通常会比原始资源更大(至少如果原始文件本身是压缩的 JPEG 或 PNG),导致上传速度较慢.

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.

UIImagePNGRepresentation 不会引入基于压缩的数据丢失,但根据图像,您仍然可能丢失数据(例如,如果原始文件是 48 位 TIFF 或使用的颜色空间不是sRGB).

UIImagePNGRepresentation doesn't introduce compression-based data loss, but depending upon the image, you may still lose data (e.g. if the original file was a 48-bit TIFF or used a colorspace other than sRGB).

这是一个问题,您是否可以在上传过程中出现一些图像质量损失和/或较大的文件大小.

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

图片尺寸:

有时您不想上传全分辨率图片.例如,您可能正在使用希望图像每边不大于 800 像素的 Web 服务.或者,如果您要上传缩略图,他们可能想要更小的东西(例如 32 像素 x 32 像素).通过调整图像大小,您可以使上传更小,从而更快(尽管有明显的质量损失).但是,如果您使用 图像大小调整算法,则使用这些 UIImageXXXRepresentation 函数创建 PNG 或 JPEG会很常见.

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 将不同的图像格式上传到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时应该使用 UIImageJPEGRepresentation 和 UIImagePNGRepresentation 将不同的图像格式上传到服务器?

基础教程推荐