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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17475392/
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-31 00:14:06  来源:igfitidea点击:

When should I use UIImageJPEGRepresentation and UIImagePNGRepresentation for uploading different image formats to the server?

iosobjective-ccocoa-touchuiimageuikit

提问by Oleksandr Karaberov

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 UIImageclass) https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html

在我的应用程序中,我必须将不同格式的图像发送到服务器(必须是UIImage类可以读取的所有文件格式)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 .pngimages I need to use UIImagePNGRepresentationand for .jpg/.jpegUIImageJPEGRepresentation. But what about other formats (.tiff,.gif, etc.)? There are only two methods for image manipulations and so many formats.

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

回答by Rob

You say:

你说:

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

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

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

不,情况不一定如此。如果你有一些独创的“数字资产”,而不是创建一个UIImage,然后利用这两个功能之一创建NSData,你会上传,你会常常只是加载NSData从原来的资产和旁路往返于UIImage在全部。如果您这样做,您就不会冒任何数据丢失的风险,这些数据会因转换为UIImage,然后再返回而导致。

There are some additional considerations, though:

不过,还有一些额外的考虑:

  1. Meta data:

    These UIImageXXXRepresentationfunctions 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.).

    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.

  2. Image quality loss and/or file size considerations:

    I'm particularly not crazy about UIImageJPEGRepresentationbecause it a lossy compression. Thus, if you use a compressionQualityvalue 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 compressionQualityvalues). And if you use a compressionQualityof 1.0, you mitigate much of the JPEG image quality loss, but the resulting NSDatacan often be bigger than the original asset (at least if the original was, itself, a compressed JPEG or PNG), resulting in slower uploads.

    UIImagePNGRepresentationdoesn'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.

  3. Image size:

    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 UIImageXXXRepresentationfunctions would be quite common.

  1. 元数据:

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

    您应该明确决定是否要删除元数据。如果没有,请不要通过 a 来回传输您的图像UIImage,而是使用原始资产。

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

    我特别不喜欢UIImageJPEGRepresentation它,因为它是一种有损压缩。因此,如果您使用compressionQuality小于 1.0的值,您可能会损失一些图像质量(接近 1.0 的值质量损失适中,值越低质量损失越严重compressionQuality)。如果您使用compressionQuality1.0 的 a,您可以减轻大部分 JPEG 图像质量损失,但结果NSData通常可能大于原始资产(至少如果原始资产本身是压缩的 JPEG 或 PNG),导致上传速度变慢.

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

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

  3. 图片尺寸:

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

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 UIImageJPGRepresentationwith quality setting of 1.0) if the original asset was not in a format accepted by the server. But the choice of using these UIImageXXXRepresentationfunctions is a question of your business requirements and what the server accepts.

简而言之,如果我试图最大程度地减少数据/质量损失,如果原始资产采用服务器接受的格式,我会上传它,如果原始资产是,我会使用UIImagePNGRepresentation(或UIImageJPGRepresentation质量设置为 1.0)不是服务器接受的格式。但是选择使用这些UIImageXXXRepresentation功能是你的业务需求和服务器接受什么的问题。

回答by lnafziger

Rob points out a lot of very good things to consider when working with images (+1), however here is an example of how to create tiff's and gif's as you asked:

Rob 指出了在处理图像 (+1) 时需要考虑的很多非常好的事情,但是这里有一个示例,说明如何按照您的要求创建 tiff 和 gif:

First, you need to link to the ImageIOlibrary (under the Build Phases of your app).

首先,您需要链接到ImageIO库(在您的应用程序的构建阶段下)。

Next you need to #import <ImageIO/ImageIO.h>at the top of your file.

接下来,您需要#import <ImageIO/ImageIO.h>在文件的顶部。

Then, the following code will convert the image for you:

然后,以下代码将为您转换图像:

// Get a reference to the image that you already have stored on disk somehow.
// If it isn't stored on disk, then you can use CGImageSourceCreateWithData() to create it from an NSData representation of your image.
NSURL *url = [[NSBundle mainBundle] URLForResource:@"01" withExtension:@"jpg"];
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(url), NULL);

// Create a URL referencing the Application Support Directory.  We will save the new image there.
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *suppurl = [fm URLForDirectory:NSApplicationSupportDirectory
                            inDomain:NSUserDomainMask
                   appropriateForURL:nil
                              create:YES
                               error:NULL];

// Append the name of the output file to the app support directory
// For tiff change the extension in the next line to .tiff
NSURL *gifURL = [suppurl URLByAppendingPathComponent:@"mytiff.gif"];

// Create the destination for the new image
// For tiff, use @"public.tiff" for the second argument of the next line (instead of @com.compuserve.gif".
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)gifURL,
                                                             (CFStringRef)@"com.compuserve.gif",
                                                             1,
                                                             NULL);
CGImageDestinationAddImageFromSource(dest, src, 0, NULL);

// Write the image data to the URL.
bool ok = CGImageDestinationFinalize(dest);
if (!ok)
    NSLog(@"Unable to create gif file.");

// Cleanup
CFRelease(src);
CFRelease(dest);

This was adapted from the code in this book.

这是改编自本书中的代码。