如何使用IOS轻松地调整/优化图像大小?

如何使用IOS轻松地调整/优化图像大小?

我的应用程序正在从网络下载一组图像文件,并将它们保存到本地iPhone磁盘上。其中一些图像的大小相当大(例如,宽度大于500像素)。由于iPhone甚至没有足够大的显示器来显示原来大小的图像,所以我计划将图像调整到更小的位置,以节省空间/性能。

此外,这些图像中的一些是JPEG,它们不会像通常的60%质量设置那样保存。

如何使用iPhoneSDK调整图片的大小,以及如何更改JPEG图像的质量设置?


慕哥6287543
浏览 826回答 3
3回答

一只甜甜圈

我们提供了一些建议,作为对这个问题..我建议使用这个职位,有关守则如下:+ (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并执行以下操作:NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);这将创建一个NSData实例,该实例包含JPEG图像的60%质量设置的原始字节。然后,可以将该NSData实例的内容写入磁盘或缓存在内存中。

元芳怎么了

调整图像大小的最简单、最直接的方法是float&nbsp;actualHeight&nbsp;=&nbsp;image.size.height;float&nbsp;actualWidth&nbsp;=&nbsp;image.size.width;float&nbsp;imgRatio&nbsp;=&nbsp;actualWidth/actualHeight; float&nbsp;maxRatio&nbsp;=&nbsp;320.0/480.0;if(imgRatio!=maxRatio){ &nbsp;&nbsp;&nbsp;&nbsp;if(imgRatio&nbsp;<&nbsp;maxRatio){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imgRatio&nbsp;=&nbsp;480.0&nbsp;/&nbsp;actualHeight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actualWidth&nbsp;=&nbsp;imgRatio&nbsp;*&nbsp;actualWidth; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actualHeight&nbsp;=&nbsp;480.0; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imgRatio&nbsp;=&nbsp;320.0&nbsp;/&nbsp;actualWidth; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actualHeight&nbsp;=&nbsp;imgRatio&nbsp;*&nbsp;actualHeight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actualWidth&nbsp;=&nbsp;320.0; &nbsp;&nbsp;&nbsp;&nbsp;}}CGRect&nbsp;rect&nbsp;=&nbsp;CGRectMake(0.0,&nbsp;0.0,&nbsp;actualWidth,&nbsp;actualHeight);UIGraphicsBeginImageContext(rect.size); &nbsp;&nbsp;&nbsp;&nbsp;[image&nbsp;drawInRect:rect];UIImage&nbsp;*img&nbsp;=&nbsp;UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();

达令说

以上方法适用于小图像,但当您试图调整一个非常大的图像大小时,您将很快耗尽内存并使应用程序崩溃。一个更好的方法是使用CGImageSourceCreateThumbnailAtIndex在没有完全解码的情况下调整图像的大小。如果您有要调整大小的图像路径,则可以使用以下命令:-&nbsp;(void)resizeImageAtPath:(NSString&nbsp;*)imagePath&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Create&nbsp;the&nbsp;image&nbsp;source&nbsp;(from&nbsp;path) &nbsp;&nbsp;&nbsp;&nbsp;CGImageSourceRef&nbsp;src&nbsp;=&nbsp;CGImageSourceCreateWithURL((__bridge&nbsp;CFURLRef)&nbsp;[NSURL&nbsp;fileURLWithPath:imagePath],&nbsp;NULL); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;To&nbsp;create&nbsp;image&nbsp;source&nbsp;from&nbsp;UIImage,&nbsp;use&nbsp;this &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;NSData*&nbsp;pngData&nbsp;=&nbsp;&nbsp;UIImagePNGRepresentation(image); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;CGImageSourceRef&nbsp;src&nbsp;=&nbsp;CGImageSourceCreateWithData((CFDataRef)pngData,&nbsp;NULL); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Create&nbsp;thumbnail&nbsp;options &nbsp;&nbsp;&nbsp;&nbsp;CFDictionaryRef&nbsp;options&nbsp;=&nbsp;(__bridge&nbsp;CFDictionaryRef)&nbsp;@{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(id)&nbsp;kCGImageSourceCreateThumbnailWithTransform&nbsp;:&nbsp;@YES, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(id)&nbsp;kCGImageSourceCreateThumbnailFromImageAlways&nbsp;:&nbsp;@YES, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(id)&nbsp;kCGImageSourceThumbnailMaxPixelSize&nbsp;:&nbsp;@(640) &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Generate&nbsp;the&nbsp;thumbnail &nbsp;&nbsp;&nbsp;&nbsp;CGImageRef&nbsp;thumbnail&nbsp;=&nbsp;CGImageSourceCreateThumbnailAtIndex(src,&nbsp;0,&nbsp;options);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;CFRelease(src); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Write&nbsp;the&nbsp;thumbnail&nbsp;at&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;CGImageWriteToFile(thumbnail,&nbsp;imagePath);}更多细节这里.
打开App,查看更多内容
随时随地看视频慕课网APP