什么时候应该使用UIImageJPEGRepresentation和UIImagePNGRepresentation上传不同的图像格式到服务器?
在我的应用程序中,我必须发送不同格式的图像到服务器(它必须是UIImage
类可以读取的所有文件格式) https://developer.apple.com/library/ios/#documentation/uikit/reference /UIImage_Class/Reference/Reference.html
问题是:我不知道什么时候应该使用这些方法。 当然很明显,对于.png
图像,我需要使用UIImagePNGRepresentation
和.jpg/.jpeg
UIImageJPEGRepresentation
。 但是其他格式( .tiff
, .gif
等)呢? 只有两种image processing方法和多种格式。
你说:
当然很明显,对于.png图像,我需要使用
UIImagePNGRepresentation
和.jpg / .jpegUIImageJPEGRepresentation
。
不,事实并非如此。 如果你有一些原始的“数字资产”,而不是创build一个UIImage
,然后使用这两个函数中的一个来创build你将要上传的NSData
,你通常会从原始资源加载NSData
,并绕过往返到一个UIImage
。 如果你这样做,你不会冒任何转换成UIImage
的数据丢失,然后再回来,可能会导致。
但是还有一些额外的考虑因素:
-
元数据:
这些
UIImageXXXRepresentation
函数剥离其元数据的图像。 有时这是一件好事(例如,你不想上传你的孩子或昂贵的小工具的照片,包括不满意的地点,哪里可以找出拍摄的地点)。 在其他情况下,您不希望元数据被丢弃(例如原始镜头的date,相机等)。您应该明确决定是否要删除元数据。 如果没有,请不要通过
UIImage
对图像进行往返,而是使用原始资源。 -
图像质量损失和/或文件大小考虑:
我特别不喜欢
UIImageJPEGRepresentation
因为它是一个有损压缩 。 因此,如果您使用的compressionQuality
值小于1.0,则可能会丢失一些图像质量(对于接近1.0的值,适度的质量损失,compressionQuality
质量值较低的质量损失更显着)。 如果使用1.0的compressionQuality
质量,则可以减轻大部分JPEG图像质量损失,但是由此产生的NSData
通常可能比原始资源大(至less如果原始文件本身是压缩的JPEG或PNG),会导致上传速度较慢。UIImagePNGRepresentation
不会引入基于压缩的数据丢失,但根据图像,您可能仍会丢失数据(例如,如果原始文件是48位TIFF或使用sRGB以外的其他颜色空间)。这是一个问题,你是否可以在上传过程中的一些图像质量损失和/或更大的文件大小。
-
图片大小:
有时候你不想上传完整分辨率的图片。 例如,您可能正在使用一种Web服务,要求每面的图像不要超过800像素。 或者,如果您要上传缩略图,则可能需要更小的内容(例如32像素x 32像素)。 通过调整图像大小,您可以使上传速度变得更小,速度更快(虽然质量明显下降)。 但是,如果您使用图像resizealgorithm ,那么使用这些
UIImageXXXRepresentation
函数创buildPNG或JPEG将是相当普遍的。
总之,如果我想尽量减less数据/质量损失,我会上传原始资产,如果它是在服务器接受的格式,我会使用UIImagePNGRepresentation
(或UIImageJPGRepresentation
与质量设置为1.0),如果原始资产没有被服务器接受的格式。 但使用这些UIImageXXXRepresentation
函数的select是您的业务需求和服务器接受的问题。
Rob在处理图片时指出了很多非常好的事情(+1),不过这里有一个如何创buildtiff和gif的例子:
首先,您需要链接到ImageIO
库(在应用程序的构build阶段下)。
接下来,您需要在文件的顶部#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);
这是从这本书的代码改编的。