贝塞尔曲线是UIkit中的一个关于图形绘制的类
贝塞尔曲线可以绘制矩形,圆形,直线,曲线,以及它们的混合图形。
系统常用的内置方法
// 创建基本路径
+ (instancetype)bezierPath;
// 创建矩形路径
+ (instancetype)bezierPathWithRect:(CGRect)rect;
// 创建椭圆路径
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
// 圆角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
// 创建指定位置的圆角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
// 绘制弧线
center:弧线圆心坐标
radius:弧线半径
startAngle:弧线起始角度
endAngle:弧线结束角度
clockwise:是否顺时针绘制
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
// 绘制一个圆弧
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
//绘制圆角图片 常用于tableView或collectionView 复用机制里面加载圆角图片卡顿 主要是因为系统默认方法直接切,是在屏幕外操作完成后,才渲染到屏幕上(离屏渲染) 解决方案:异步绘制,主线程渲染
-(void)drawRect:(CGRect)rect
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
UIImage *image=[UIImage imageNamed:@"share_code_pro"];
CGRect rect1 = CGRectMake(0, 0, 100, 100);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, [UIScreen mainScreen].scale);
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//构建bezier曲线
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect1 byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
//
CGContextAddPath(ctx,path.CGPath);
//裁剪
CGContextClip(ctx);
//将图片贴上去
[image drawInRect:rect1];
CGContextDrawPath(ctx, kCGPathFillStroke);
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭图形上下文
UIGraphicsEndImageContext();
imageView.image=newImage;
[self addSubview:imageView];
}