objective-c 将 UIImage 大小减少到可管理的大小(减少字节)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/487316/
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
Reduce UIImage size to a manageable size (reduce bytes)
提问by Mustafa
I want to reduce the number of bytes of an image captured by the device, since i believe the _imageScaledToSizedoes not reduce the number of bytes of the picture (or does it?) - i want to store a thumbnail of the image in a local dictionary object and can't afford to put full size images in the dictionary. Any idea?
我想减少设备捕获的图像的字节数,因为我相信这_imageScaledToSize不会减少图片的字节数(或者是吗?) - 我想将图像的缩略图存储在本地字典中对象并且无法将全尺寸图像放入字典中。任何的想法?
回答by Brad Larson
If you wish to simply compress your UIImage, you can use
如果你想简单地压缩你的 UIImage,你可以使用
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
to generate an NSData version of your image encoded as a PNG (easily inserted into an NSDictionary or written to disk), or you can use
生成编码为 PNG 的图像的 NSData 版本(很容易插入到 NSDictionary 或写入磁盘),或者您可以使用
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
to do the same, only in a JPEG format. The second parameter is the image quality of the JPEG. Both of these should produce images that are smaller, memory-wise, than your UIImage.
做同样的事情,只是以 JPEG 格式。第二个参数是JPEG的图像质量。这两者都应该生成比 UIImage 更小的内存图像。
Resizing a UIImage to create a smaller thumbnail (pixels-wise) using published methods is a little trickier. _imageScaledToSize is from the private API, and I'd highly recommend you not use it. For a means that works within the documented methods, see this post.
使用已发布的方法调整 UIImage 大小以创建较小的缩略图(像素级)有点棘手。_imageScaledToSize 来自私有 API,我强烈建议您不要使用它。有关在记录的方法中有效的方法,请参阅此帖子。
回答by radesix
I ran into this problem the other day and did quite a bit of research. I found an awesome solution complete with code here:
前几天我遇到了这个问题,并做了很多研究。我在这里找到了一个带有代码的很棒的解决方案:
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
回答by Mike Abdullah
You need to draw the image into a graphics context at a smaller size. Then, release the original image.
您需要以较小的尺寸将图像绘制到图形上下文中。然后,释放原始图像。
回答by mmr
When you say 'physical size', are you talking about a print? Because you can just change the printer page size.
当您说“物理尺寸”时,您指的是印刷品吗?因为您可以更改打印机页面大小。
Are you talking about the number of pixels used to capture the image? As in, if you have a pixel array of 3000x2000, and you only want 150x150, then you can crop the images. At the time of capture, if you have a scientific imager, then you can just set the area that will be captured. The camera driver would include instructions for that. If you want to capture 3000x2000 in 1500x1000, you can try to bin the image, if that's what you need.
您是在谈论用于捕获图像的像素数吗?例如,如果您有一个 3000x2000 的像素阵列,而您只想要 150x150,那么您可以裁剪图像。在捕获时,如果您有科学成像仪,那么您只需设置将要捕获的区域即可。相机驱动程序将包含相关说明。如果您想在 1500x1000 中捕获 3000x2000,您可以尝试合并图像,如果这是您需要的。
Or, you can use resampling post-capture in order to make the image smaller. One such algorithm is bicubic resampling, also linear resampling-- there are many variations.
或者,您可以使用重新采样后捕获来缩小图像。一种这样的算法是双三次重采样,也是线性重采样——有很多变化。
I'm thinking this last is what you're most interested in... in which case, check out this Wikipedia pageon the algorithm. Or, you can go to FreeImageand get a library that will read in the image and can also resize images.
我认为最后一个是您最感兴趣的......在这种情况下,请查看有关算法的维基百科页面。或者,您可以转到FreeImage并获取一个库,该库将读取图像并且还可以调整图像大小。
回答by user2067021
UIImageJPEGRepresentationdoes the trick but I find that using the ImageIOframework often gets significantly better compression results for the same quality setting. It may be slower, but depending on your use case this may not be an issue.
UIImageJPEGRepresentation确实有用,但我发现使用该ImageIO框架通常会在相同质量设置下获得明显更好的压缩结果。它可能会更慢,但根据您的用例,这可能不是问题。
(Code adapted for NSDatafrom this blog post by Zachary West).
(代码改编NSData自Zachary West 的这篇博文)。
#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/ImageIO.h>
...
+ (NSData*)JPEGDataFromImage:(UIImage*)image quality:(double)quality
{
CFMutableDataRef outputImageDataRef = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef imageDestinationRef = CGImageDestinationCreateWithData(outputImageDataRef, kUTTypeJPEG, 1, NULL);
NSDictionary* properties = @{
(__bridge NSString*)kCGImageDestinationLossyCompressionQuality: @(quality)
};
CGImageDestinationSetProperties(imageDestinationRef, (__bridge CFDictionaryRef)properties);
CGImageDestinationAddImage(imageDestinationRef, image.CGImage, NULL);
CGImageDestinationFinalize(imageDestinationRef);
CFRelease(imageDestinationRef);
NSData* imageData = CFBridgingRelease(outputImageDataRef);
return imageData;
}

