调整 UIImage 大小的最简单方法? [英] The simplest way to resize an UIImage?
问题描述
在我的 iPhone 应用程序中,我用相机拍了一张照片,然后我想将其大小调整为 290*390 像素.我正在使用这种方法来调整图像大小:
In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :
UIImage *newImage = [image _imageScaledToSize:CGSizeMake(290, 390)
interpolationQuality:1];
它运行良好,但它是一个未记录的功能,所以我不能再在 iPhone OS4 上使用它.
It works perfectly, but it's an undocumented function, so I can't use it anymore with iPhone OS4.
那么……调整 UIImage 大小的最简单方法是什么?
So... what is the simplest way to resize an UIImage ?
推荐答案
最简单的方法是设置你的 UIImageView
的 frame 并将 contentMode
设置为调整大小选项.
The simplest way is to set the frame of your UIImageView
and set the contentMode
to one of the resizing options.
或者您可以使用此实用方法,如果您确实需要调整图像大小:
Or you can use this utility method, if you actually need to resize an image:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
示例用法:
#import "MYUtil.h"
…
UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];
这篇关于调整 UIImage 大小的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!