如何使用iOS轻松调整大小/优化图像大小? [英] How to easily resize/optimize an image size with iOS?

查看:91
本文介绍了如何使用iOS轻松调整大小/优化图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在从网络下载一组图像文件,并将其保存到本地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.

这篇关于如何使用iOS轻松调整大小/优化图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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