原文地址:http://blog.youkuaiyun.com/think12/article/details/8886135
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。
例如:
1。显示正常的图片
- _image = [[UIImageView alloc] init];
- image = [UIImage imageNamed:@"12.jpeg"];
- _image.backgroundColor = [UIColor brownColor];
- _image.clipsToBounds = YES;
- _image.frame = CGRectMake(100, 130, 100, 100);
- _image.contentMode = UIViewContentModeScaleToFill;
- [self.view addSubview:_image];
2。
- _image.contentMode = UIViewContentModeScaleAspectFill;
3。
- _image.contentMode = UIViewContentModeScaleAspectFit;
本文详细介绍了UIImageView的contentMode属性,该属性决定了图片在UIImageView中的显示方式。包括如何保持图片比例不变的同时填充UIImageView(UIViewContentModeScaleAspectFill),如何确保图片完全可见但可能留白(UIViewContentModeScaleAspectFit),以及可能导致图片变形的填充方式(UIViewContentModeScaleToFill)。通过不同的设定,可以实现图片的居中、靠边等效果。
3470

被折叠的 条评论
为什么被折叠?



