设置圆角的方式

#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;
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容