UIImageView中图片尺寸模式

本文详细解析了UIImageView中图片尺寸模式的枚举,包括:UIViewContentModeScaleToFill、UIViewContentModeScaleAspectFit、UIViewContentModeScaleAspectFill等13种模式。每种模式都描述了如何在不改变图片比例或保持在UIImageView内显示图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尺寸模式枚举:

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,
};

上面每个枚举的意思详解如下:

测试代码:

    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(100, 200, 200, 300);
    imageView.image = [UIImage imageNamed:@"dog.png"];
    [imageView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:imageView];
默认情况:

1、UIViewContentModeScaleToFill   伸缩至高度和宽度与UIImageView的大小一致

    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(100, 200, 200, 300);
    imageView.image = [UIImage imageNamed:@"dog.png"];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [imageView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:imageView];

2、 UIViewContentModeScaleAspectFit    不改变图片高宽比例,且不超过UIImageView的尺寸范围的情况下 伸缩至最大

3、UIViewContentModeScaleAspectFill    在图片比例不变的情况下伸缩至最大,但最多只允许宽或高其中一个超出UIImageView的尺寸


4、UIViewContentModeCenter    不改变图片尺寸,在UIImageView正中央

5、UIViewContentModeTop   不改变图片尺寸,但图片在UIImageView的顶部中央

6、UIViewContentModeBottom     不改变图片尺寸,但图片在UIImageView的底部中央

7、UIViewContentModeLeft       不改变图片尺寸,但图片在UIImageView的左边中央

8、UIViewContentModeRight      不改变图片尺寸,但图片在UIImageView的右边中央

9、UIViewContentModeTopLeft     不改变图片尺寸,但图片在UIImageView的左上角

10、UIViewContentModeTopRight     不改变图片尺寸,但图片在UIImageView的右上角

11、UIViewContentModeBottomLeft     不改变图片尺寸,但图片在UIImageView的左下角

12、UIViewContentModeBottomRight     不改变图片尺寸,但图片在UIImageView的右下角

13、UIViewContentModeRedraw   图片充满UIImageView,但是只要UIImageView的bounds属性发送改变就调用setNeedsDisplay方法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值