如前所述這應該最有可能不能使用OpenGL做,但即使...
這個問題大多數人都越來越其中圖像應顯示與解決方案看起來像這樣的矩形:
- (CGRect)fillSizeForSource:(CGRect)source target:(CGRect)target
{
if(source.size.width/source.size.height > target.size.width/target.size.height)
{
// keep target height and make the width larger
CGSize newSize = CGSizeMake(target.size.height * (source.size.width/source.size.height), target.size.height);
return CGRectMake((target.size.width-newSize.width)*.5f, .0f, newSize.width, newSize.height);
}
else
{
// keep target width and make the height larger
CGSize newSize = CGSizeMake(target.size.width, target.size.width * (source.size.height/source.size.width));
return CGRectMake(.0f, (target.size.height-newSize.height)*.5f, newSize.width, newSize.height);
}
}
- (CGRect)fitSizeForSource:(CGRect)source target:(CGRect)target
{
if(source.size.width/source.size.height < target.size.width/target.size.height)
{
// keep target height and make the width smaller
CGSize newSize = CGSizeMake(target.size.height * (source.size.width/source.size.height), target.size.height);
return CGRectMake((target.size.width-newSize.width)*.5f, .0f, newSize.width, newSize.height);
}
else
{
// keep target width and make the height smaller
CGSize newSize = CGSizeMake(target.size.width, target.size.width * (source.size.height/source.size.width));
return CGRectMake(.0f, (target.size.height-newSize.height)*.5f, newSize.width, newSize.height);
}
}
我沒有測試這個。
或者既然你是在iOS上,只需創建一個具有所需大小的圖像視圖,添加一個圖像,然後獲取圖像的屏幕截圖。
OpenGL及其衍生物是用於繪製東西的API,最終顯示在屏幕上。核心目的是在實時圖形渲染中使用。你的任務是通用圖像處理,這對於OpenGL是可能的,但通常使用專用圖像操作庫更容易完成。 – datenwolf 2014-11-01 12:32:24
@datenwolf好的,你能說出一些嗎? – efimovdk 2014-11-01 13:00:16