UIImageview的contentmode介绍

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值