问题描述
我的应用程序正在从网络下载一组图像文件,并将它们保存到本地 iPhone 磁盘.其中一些图像的尺寸非常大(例如,宽度大于 500 像素).由于 iPhone 的显示屏甚至没有足够大的显示屏以原始尺寸显示图像,因此我计划将图像大小调整为小一点以节省空间/性能.
My application is downloading a set of image files from the network, and saving them to the local iPhone disk. Some of those images are pretty big in size (widths larger than 500 pixels, for instance). Since the iPhone doesn't even have a big enough display to show the image in its original size, I'm planning on resizing the image to something a bit smaller to save on space/performance.
此外,其中一些图像是 JPEG 格式,它们不会保存为通常的 60% 质量设置.
Also, some of those images are JPEGs and they are not saved as the usual 60% quality setting.
如何使用 iPhone SDK 调整图片大小,以及如何更改 JPEG 图像的质量设置?
How can I resize a picture with the iPhone SDK, and how can I change the quality setting of a JPEG image?
推荐答案
提供了一些建议作为 这个问题.我曾建议使用 这篇文章 中描述的技术,以及相关代码:
A couple of suggestions are provided as answers to this question. I had suggested the technique described in this post, with the relevant code:
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
就图像存储而言,与 iPhone 一起使用的最快图像格式是 PNG,因为它对该格式进行了优化.但是,如果您想将这些图像存储为 JPEG,您可以使用 UIImage 并执行以下操作:
As far as storage of the image, the fastest image format to use with the iPhone is PNG, because it has optimizations for that format. However, if you want to store these images as JPEGs, you can take your UIImage and do the following:
NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);
这会创建一个 NSData 实例,其中包含 JPEG 图像的原始字节,质量设置为 60%.然后可以将该 NSData 实例的内容写入磁盘或缓存在内存中.
This creates an NSData instance containing the raw bytes for a JPEG image at a 60% quality setting. The contents of that NSData instance can then be written to disk or cached in memory.
这篇关于如何使用 iOS 轻松调整/优化图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!