24

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

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

4

2 回答 2

52

你说:

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

不,不一定是这样。如果您有一些原始的“数字资产”,而不是创建一个UIImage然后使用这两个函数之一来创建NSData您将上传的,您通常只需NSData从原始资产加载并绕过往返UIImage于全部。如果您这样做,则不会冒任何数据丢失的风险,即转换为 a UIImage,然后再返回可能导致的数据丢失。

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

  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. 图片尺寸:

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

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

于 2013-07-04T17:45:51.440 回答
2

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

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

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

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

// 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);

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

于 2013-07-05T03:34:11.810 回答