第一種方法:通過設置layer的屬性
這種方法簡單,但是很影響性能,特別是在UIcollectionView中展示大量圓角圖片,一般在正常的開發中使用很少
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; //設置圓角 imageView.layer.cornerRadius = imageView.frame.size.width / 2; //將多余的部分切掉 imageView.layer.masksToBounds = YES; [self.view addSubview:imageView];
第二種方法:使用CAShapeLayer和UIBezierPath設置圓角,第二種最好,對內存的消耗最少,而且渲染快速
UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectEdgeLeft |
UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = view.bounds;
maskLayer.path = path.CGPath;
view.layer.mask = maskLayer;
UIRectEdgeLeft 可以根據需求設置需要設置圓角的位置。

