1.直接用視圖中layer中的兩個屬性來設置圓角,這種方法比較簡單,但是及其影響性能不推薦:
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
self.imageView.layer.cornerRadius = 5;
self.imageView.layer.masksToBounds = YES;
2.通過layer和bezierPath 設置圓角
- (void)setLayerAndBezierPathCutCircularWithView:(UIView *) view
{
// 創建BezierPath 并設置角 和 半徑
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.frame = view.bounds;
layer.path = path.CGPath;
view.layer.mask = layer;
}
3.通過Graphics 和 BezierPath 設置圓角
- (void)setGraphicsCutCirculayWithView:(UIImageView *) view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:5] addClip];
[view drawRect:view.bounds];
view.image = UIGraphicsGetImageFromCurrentImageContext();
// 結束
UIGraphicsEndImageContext();
}