【发布时间】:2011-03-20 22:06:57
【问题描述】:
我想编写一个函数来缩小图像以适应指定的范围。例如,我想调整 2000x2333 图像的大小以适应 1280x800。必须保持纵横比。我想出了以下算法:
NSSize mysize = [self pixelSize]; // just to get the size of the original image
int neww, newh = 0;
float thumbratio = width / height; // width and height are maximum thumbnail's bounds
float imgratio = mysize.width / mysize.height;
if (imgratio > thumbratio)
{
float scale = mysize.width / width;
newh = round(mysize.height / scale);
neww = width;
}
else
{
float scale = mysize.height / height;
neww = round(mysize.width / scale);
newh = height;
}
而且它似乎奏效了。嗯......似乎。但是后来我尝试将 1280x1024 图像的大小调整为 1280x800 的边界,它给了我 1280x1024 的结果(这显然不适合 1280x800)。
有人知道这个算法应该如何工作吗?
【问题讨论】: