[BS-24] UIImageView的contentMode属性

UIImageView的contentMode属性

 
所有的UIView都有个contentMode属性,UIImageView继承自UIView,我们在使用UIImageView时,经常要考虑这些图片是否需要拉伸,采用哪种拉伸模式等,这就要求我们正确理解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: 将图片等比例伸缩,直到能把整张图片装进ImageView(图片不会变形,但ImageView中图片四周可能有空白)

UIViewContentModeScaleAspectFill: 将图片等比例伸缩,直到宽度等于UIImageView的宽度or图片的高度等于UIImageView的高度为止,然后将图片居中显示

UIViewContentModeRedraw : 调用了setNeedsDisplay方法时,就会将图片重新渲染.

UIViewContentModeCenter:居中显示

......简单的看英文就能懂的就不一一介绍了!


我们还可以设置超出边框的内容都剪掉:

topView.clipsToBounds = YES;

总结:

  • 凡是带有Scale单词的,图片都会拉伸
  • 同时带有Fill单词的,都会将ImageView填满,图片四周没有空隙
  • 凡是带有Aspect单词的,图片都会保持原来的宽高比,图片不会变形

举例:新浪微博里面的图片,就是采用了UIViewContentModeScaleAspectFill这种模式来展示图片的。

图解:

contenMode

 
 
原文地址:https://www.cnblogs.com/stevenwuzheng/p/5517288.html