我在很多帖子里看到过通过保持纵横比来调整图像大小.这些函数在调整大小时使用矩形的固定点(宽度和高度).但是在我的项目中,我需要仅根据宽度调整视图大小,高度应该根据纵横比自动获取. 有没有人能帮我做到这一点.
我在很多帖子里看到过通过保持纵横比来调整图像大小.这些函数在调整大小时使用矩形的固定点(宽度和高度).但是在我的项目中,我需要仅根据宽度调整视图大小,高度应该根据纵横比自动获取. 有没有人能帮我做到这一点.
Srikar的方法非常有效,如果你知道你的新尺码的高度和宽度都是and. 例如,如果您只知道要zoom 到的宽度,而不关心高度,则首先必须计算高度的比例因子.
+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = i_width / oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}