Simulator Screen Shot - iPhone SE (2nd generation) - 2020-05-09 at 16.31.23.png
- (void)viewDidLoad
{
[super viewDidLoad];
[self cus_bezierPath];
}
#pragma mark - 方法一:bezierPath方法
-(void)cus_bezierPath{
//获取图片
UIImage* image = [UIImage imageNamed:@"me"];
CGFloat imgWH = image.size.width;
CGFloat border = 10;
CGFloat bgWH = imgWH+border*2;
CGSize bgSize = CGSizeMake(bgWH, bgWH);
//开启图片类型的图形上下文
UIGraphicsBeginImageContextWithOptions(bgSize, NO, 0);
//画圆形路径
UIBezierPath *bp = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, bgWH, bgWH)];
//设置填充颜色
[[UIColor yellowColor]set];
//填充颜色
[bp fill];
//画icon
UIBezierPath *imgBP = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imgWH, imgWH)];
//将其添加至裁剪区域
[imgBP addClip];//默认使用非零环绕规则(‘usesEvenOddFillRule’属性用来设置使用‘非零环绕规则’还是‘奇偶规则’填充或裁剪)
//图片画至context的裁剪区域中
[image drawAtPoint:CGPointMake(border, border)];
//从context取出图片
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
self.imgv.image = icon;
//关闭图片类型的图形上下文
UIGraphicsEndImageContext();
}
#pragma mark - 方法二:context方法
-(void)cus_context{
UIImage* image = [UIImage imageNamed:@"me"];
CGFloat imgWH = image.size.width;
CGFloat border = 10;
CGFloat bgWH = imgWH+border*2;
CGSize bgSize = CGSizeMake(bgWH, bgWH);
UIGraphicsBeginImageContextWithOptions(bgSize, NO, 0);
CGContextRef ref = UIGraphicsGetCurrentContext();
CGFloat arcCenterX = bgWH/2;
CGFloat arcRadius = imgWH/2 + border/2;
CGContextAddArc(ref, arcCenterX, arcCenterX, arcRadius, 0, M_PI*2, 1);
//设置路径宽度
CGContextSetLineWidth(ref, border);
//设置路径颜色
CGContextSetStrokeColorWithColor(ref, [UIColor redColor].CGColor);
//渲染
CGContextStrokePath(ref);
CGFloat arcImgRadius = imgWH/2;
CGContextAddArc(ref, arcCenterX, arcCenterX, arcImgRadius, 0, M_PI*2, 1);
//裁剪显示区域
CGContextClip(ref);
//画图
[image drawAtPoint:CGPointMake(border, border)];
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
self.imgv.image = icon;
//关闭图片类型上下文
UIGraphicsEndImageContext();
}
网友评论