3 回答

TA貢獻1860條經驗 獲得超9個贊
設置屏幕矩形后,請執行以下類似操作,以決定在哪個矩形上繪制圖像:
float hfactor = value.bounds.size.width / screenRect.size.width;
float vfactor = value.bounds.size.height / screenRect.size.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = value.bounds.size.width / factor;
float newHeight = value.bounds.size.height / factor;
// Then figure out if you need to offset it to center vertically or horizontally
float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;
CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
如果您不想放大小于screenRect的圖像,請確保factor大于或等于1(例如factor = fmax(factor, 1))。
要獲得黑色背景,您可能只想將上下文顏色設置為黑色,并在繪制圖像之前調用fillRect。

TA貢獻1785條經驗 獲得超8個贊
我得到“的功能隱式聲明的‘最大’”由編譯器拋出,即使我記得,包括math.h中(我用的XCode 4.0)更改:float factor = max( …以float factor = MAX( …全部大寫)解決了這個問題。FWIW之所以需要重新縮放UIImage(而不是UIImageView),是因為我將圖像設置為默認樣式的UITableCell。這有助于我避免實現自定義UITableCell。感謝Frank提供的出色解決方案!
- 3 回答
- 0 關注
- 578 瀏覽
添加回答
舉報