UIImage *img = [UIImage ly_imageWithColor:[UIColor redColor]];
self.imgView = [[UIImageView alloc] initWithImage:img];
self.imgView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:self.imgView];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imgView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imgView.bounds;
maskLayer.path = maskPath.CGPath;
self.imgView.layer.mask = maskLayer;
關(guān)于參數(shù):
第二個(gè)參數(shù)byRoundingCorners:(UIRectCorner)corners允許指定矩形的部分角為圓角,而其余的角為直角,取值來自枚舉:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
其最后一個(gè)參數(shù)cornerRadii:(CGSize)cornerRadii指定了圓角的半徑,但這里需要注意,這個(gè)參數(shù)的取值是 CGSize類型,也就意味著這里需要給出的是橢圓的半徑。