使用iPhone SDK调整/优化图像大小的最简单的方法是什么? [英] What's the easiest way to resize/optimize an image size with the iPhone SDK?

查看:100
本文介绍了使用iPhone SDK调整/优化图像大小的最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在从网络下载一组图像文件,并将其保存到本地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实例,它包含60%质量设置下JPEG图像的原始字节。然后可以将该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.

这篇关于使用iPhone SDK调整/优化图像大小的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆