UIImageview的contentmode介绍

官方解释:A flag used to determine how a lays out its content when its bounds change.//当一个view的bounds变化的时候用于决定其内容怎么变化。
定义:

@property(nonatomic) UIViewCOntentMode contentMode

这一属性通常用于实现可调整大小的控制,通常与contentStretch属性一起使用。通过使用这一属性来决定你扩充的模式,从而避免了之前每次都要重新对view的内容进行重画。

NOTE
你可以使用 setNeedsDisplay或者setNeedsDisplayInRect:方法来强制对一个view进行重绘。

UIViewContentMode的默认属性是UIViewContentModeScaleToFill

typedef NS_ENUM(NSInteger, UIViewContentMode) {
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,
};
其中主要分为:ScaleToFill,ScaleAspectFit,ScaleAspectFill三种。

  • ScaleToFill为将图片按照整个区域进行拉伸(会破坏图片的比例)
  • ScaleAspectFit将图片等比例拉伸,可能不会填充满整个区域
  • ScaleAspectFill将图片等比例拉伸,会填充整个区域,但是会有一部分过大而超出整个区域。
    至于Top,Left,Right等等就是将图片在view中的位置进行调整。

通过图片来进行理解:
UIViewContentModeScaleToFill

UIViewContentModeScaleToFill
UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill
UIViewContentModeScaleAspectFill
UIViewContentModeScaleAspectFill

UIViewContentModeRight
UIViewContentModeRight
UIViewContentModeRight

图中橙色边框是imageview的边框,我们可以发现当使用scaleaspectfill的时候图片会超出边框区域。
基本介绍如上,具体代码可见gitHub地址:ViewContentModeDemo.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容