我怎样才能resize的UIImage到特定的大小

在我的应用程序中,我正在使用相机拍摄照片并将其显示在320 * 320的UIView 。 但是由于图像的分辨率比UIView的图像分辨率还要高。 有什么办法可以调整图像大小吗?

这是如何调整图像的大小,同时保持其高宽比。 下面的代码来自UIImage一个类别:

 + (UIImage*)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { float heightToWidthRatio = image.size.height / image.size.width; float scaleFactor = 1; if(heightToWidthRatio > 0) { scaleFactor = newSize.height / image.size.height; } else { scaleFactor = newSize.width / image.size.width; } CGSize newSize2 = newSize; newSize2.width = image.size.width * scaleFactor; newSize2.height = image.size.height * scaleFactor; UIGraphicsBeginImageContext(newSize2); [image drawInRect:CGRectMake(0,0,newSize2.width,newSize2.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

我这样做,但首先我启用照片select器可编辑,所以它是默认的1:1方面,如下所示:

 imgPicker.allowsImageEditing = YES; //I think this is what you're really looking for 

然后我使用这种方法调整图像大小:

 -(UIImage*)resizeImage:(UIImage*)image{ CGSize newSize = CGSizeMake(480.0, 480.0); UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }