- 我們在開發中,經常要在view,tableView,等顯示圖片,比如我們發微博(帶有圖片的),那麼我們常常要考慮如何處理這些圖片,是否拉伸,拉伸到什麼樣的效果等等,常常是需要考慮的問題
- 其實主要的還是要理解UIImageView的ContentMode的這些參數,這些參數一般就能滿分我們的開發需求
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
- 那我們接下來,就來說明一下,這些值都代表什麼意思
- UIViewContentModeScaleToFill:圖片拉伸至填充這個UIImageView(圖片可能變形)
- UIViewContentModeScaleAspectFit : 圖片拉伸至完全顯示在UIImageView裏面爲止(圖片不會變形)
- UIViewContentModeScaleAspectFill : 圖片拉伸至 圖片的寬度等於UIImageView的寬度 或者 圖片的高度等於UIImageView的高度爲止,然後將圖片居中顯示
- UIViewContentModeRedraw : 調用了setNeedsDisplay方法時,就會將圖片重新渲染
- UIViewContentModeCenter:居中顯示
- 。。。。別的跟這個也是一樣的了,用的少
我們還可以設置超出邊框的內容都剪掉:
topView.clipsToBounds = YES;
總結一下:
- 凡是帶有Scale單詞的,圖片都會拉伸
- 凡是帶有Aspect單詞的,圖片都會保持原來的寬高比,圖片不會變形
我記得:新浪微博裏面的圖片,就是採用了UIViewContentModeScaleAspectFill這種模式來展示圖片的
最後上傳一張圖片 ,幫助大家來理解: