UIImageView中图片尺寸模式

尺寸模式枚举:

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方法




版权声明:本文为lvdezhou原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。