主要內(nèi)容:
1莫杈、contentMode
2、layer contentsRect
contentMode 就是描述內(nèi)容的填充模式。
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,
};
contentsRect
/* A rectangle in normalized image coordinates defining the
- subrectangle of the `contents' property that will be drawn into the
- layer. If pixels outside the unit rectangles are requested, the edge
- pixels of the contents image will be extended outwards. If an empty
- rectangle is provided, the results are undefined. Defaults to the
- unit rectangle [0 0 1 1]. Animatable. */
在現(xiàn)在的圖像坐標(biāo)系統(tǒng)中,contentsRect描述了一個在layer層重畫的一個子矩形棒呛。控制圖層內(nèi)容的顯示域携。
在實際應(yīng)用中,類似微信朋友圈鱼喉,照片的顯示秀鞭,當(dāng)顯示多張圖片的時候趋观,由于我們會固定顯示view 的大小為一個矩形,但是如果我們直接將照片放入會發(fā)生變形锋边。
但是在大多數(shù)情況下面我們都希望能夠不變形皱坛。圖片按照長寬來分,大概分為3種豆巨,W>H , W=H, W <H剩辟, 這三種情況。舉個例子往扔,在我們要顯示圖片的UIImageView是一個W=H的情況贩猎。所以要求不變形,主要有兩個方法:
例子:
1: W > H
UIImageView *squerImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, 300, 100, 100)];
squerImage.layer.borderColor = [UIColor redColor].CGColor;
squerImage.layer.borderWidth = 1;
squerImage.layer.masksToBounds = true;
UIImage *widthImage = [UIImage imageNamed:@"zisehua.jpg"];
squerImage.image = widthImage;
squerImage.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:squerImage];
結(jié)果:
2: H > W
UIImageView *qingting = [[UIImageView alloc]initWithFrame:CGRectMake(30, 430, 100, 100)];
qingting.layer.borderColor = [UIColor redColor].CGColor;
qingting.layer.borderWidth = 1;
qingting.layer.masksToBounds = true;
UIImage *qingtingImage = [UIImage imageNamed:@"qingting.jpg"];
qingting.image = qingtingImage;
qingting.layer.contentsRect = CGRectMake(0, 0,1, qingtingImage.size.width/qingtingImage.size.height);
qingting.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:qingting];
結(jié)果: