#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *pictureImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置圆角各个方法在下面列出
}
// 基本方式添加圆角
self.pictureImageView.layer.cornerRadius = 20;
self.pictureImageView.layer.masksToBounds = YES;
//使用UIBezierPath和core graphics画一个圆角
UIGraphicsBeginImageContextWithOptions(self.pictureImageView.bounds.size, YES, 0.0);
[[UIBezierPath bezierPathWithRoundedRect:self.pictureImageView.bounds cornerRadius:20]addClip];
[self.pictureImageView drawRect:self.pictureImageView.bounds];
self.pictureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//使用Core Graphics框架画出一个圆
UIGraphicsBeginImageContextWithOptions(self.pictureImageView.bounds.size, YES, 1.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, self.pictureImageView.bounds.size.width, self.pictureImageView.bounds.size.height);
CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx);
[self.pictureImageView.image drawInRect:rect];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.pictureImageView.image = newImage;
// 使用CAShapeLayer和UIBezierPath设置圆角
UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.pictureImageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.pictureImageView.bounds;
maskLayer.path = maskPath.CGPath;
self.pictureImageView.layer.mask = maskLayer;
// 制定角为圆角
UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.pictureImageView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.pictureImageView.bounds;
maskLayer.path = maskPath.CGPath;
self.pictureImageView.layer.mask = maskLayer;