本文章的目的是闡明在UIImageView中如何正確的拉伸圖片痢掠。
UIViewContentMode定義
typedef NS_ENUM(NSInteger, UIViewContentMode) {
//The option to scale the content to fit the size of itself by changing the aspect ratio of the content if necessary.
UIViewContentModeScaleToFill,
//The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
UIViewContentModeScaleAspectFit,
//The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
//The option to redisplay the view when the bounds change by invoking the setNeedsDisplay method.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
需要說明的是但凡包含Scale這個單詞的值, 都會對原有的圖片進行縮放驱犹。
這里我們著重說明前三個值
UIViewContentModeScaleToFill
鋪滿整個畫布,圖片會變形
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit 效果
圖片不變形足画,畫布會留有空白
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill 效果
圖片不變形雄驹,鋪滿整個畫布,圖片會被裁剪
UIViewContentModeScaleAspectFill
另外UIView中的contentMode對應的是CALayer中的contentGravity淹辞,那么二者之間的對應關系是什么呢
UIViewContentModeScaleToFill, => kCAGravityResizeAspectFill
UIViewContentModeScaleAspectFit, => kCAGravityResizeAspect
UIViewContentModeScaleAspectFill, => kCAGravityResize
UIViewContentModeRedraw, => ???
UIViewContentModeCenter, => kCAGravityCenter
UIViewContentModeTop, => kCAGravityTop
UIViewContentModeBottom, => kCAGravityBottom
UIViewContentModeLeft, => kCAGravityLeft
UIViewContentModeRight, => kCAGravityRight
UIViewContentModeTopLeft, => kCAGravityTopLeft
UIViewContentModeTopRight, => kCAGravityTopRight
UIViewContentModeBottomLeft, => kCAGravityBottomLeft
UIViewContentModeBottomRight, => kCAGravityBottomRight
};