最近在做一个类似更换头像的功能,终于解决啦,开心
在View上掏出一个空心的各种形状
UIView* aView = [[UIViewalloc] initWithFrame:CGRectMake(100,80, 100, 100)];
[self.viewaddSubview:aView];
//用来标识layer的绘图是否正确
//设置边界样式
aView.layer.borderWidth =1.0;
aView.layer.borderColor = [UIColorblackColor].CGColor;
CAShapeLayer* cropLayer = [[CAShapeLayeralloc] init];
[aView.layeraddSublayer:cropLayer];
CGMutablePathRef path =CGPathCreateMutable();
//空余区域的位置大小,颜色设置aView的背景色
CGRect cropRect = CGRectMake(20, 30, 60, 40);
CGPathAddRect(path, nil, aView.bounds);
CGPathAddRect(path, nil, cropRect);
//CGPathAddRoundedRect(path, nil, cropRect, 导圆角的宽度, 导圆角的高度);
//重点
//将绘制图层的样式反过来
[cropLayersetFillRule:kCAFillRuleEvenOdd];
[cropLayersetPath:path];
//被填充图层的颜色
[cropLayer setFillColor:[[UIColorredColor] CGColor]];