第一种方式:
注意:此种方式下,如果是滚动的列表就会出现卡顿的性能问题。代码如下:
- (void)initWithTheItemsView {
CAShapeLayer * shapLayer = [CAShapeLayer layer];
shapLayer.path = [self userImageViewBgViewPath].CGPath;
self.userImgaeBgView.layer.mask = shapLayer;
CAShapeLayer * strokeLayer = [CAShapeLayer layer];
strokeLayer.path = [self userImageViewBgViewPath].CGPath;
strokeLayer.strokeColor = [UIColor whiteColor].CGColor;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.lineWidth = 4;
strokeLayer.lineCap = kCALineCapSquare;
[self.userImgaeBgView.layer addSublayer:strokeLayer];
CAShapeLayer * inShapLayer = [CAShapeLayer layer];
inShapLayer.path = [self userImageVPath].CGPath;
self.userImageV.layer.mask = inShapLayer;
}
- (UIBezierPath *)userImageVPath {
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat width = self.userImageV.bounds.size.width;
CGFloat height = self.userImageV.bounds.size.height;
path.lineWidth = 0.0001;
[path moveToPoint:CGPointMake(width/2, 0)];
[path addLineToPoint:CGPointMake(0, height/4)];
[path addLineToPoint:CGPointMake(0, 3*height/4.)];
[path addLineToPoint:CGPointMake(width/2, height)];
[path addLineToPoint:CGPointMake(width, 3*height/4)];
[path addLineToPoint:CGPointMake(width, height/4)];
[path closePath];
return path;
}
- (UIBezierPath *)userImageViewBgViewPath {
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat width = self.userImgaeBgView.bounds.size.width;
CGFloat height = self.userImgaeBgView.bounds.size.height;
[path moveToPoint:CGPointMake(width/2, 0)];
[path addLineToPoint:CGPointMake(0, height/4)];
[path addLineToPoint:CGPointMake(0, 3*height/4)];
[path addLineToPoint:CGPointMake(width/2, height)];
[path addLineToPoint:CGPointMake(width, 3*height/4)];
[path addLineToPoint:CGPointMake(width, height/4)];
[path closePath];
return path;
}
第二种方式:(推荐)
代码如下:
- (UIImage *)addBpolygonToImage:(UIImage *)image withLineWidth:(CGFloat)lineW andColor:(UIColor *)color{
//
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
//
CGContextRef ctx = UIGraphicsGetCurrentContext();
//
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
//
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);
//
CGFloat lineWidth = lineW;
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat width = image.size.width;
CGFloat x, y;
CGFloat radius = width / 2 - lineWidth / 2;
CGPoint origin = CGPointMake(width / 2, width / 2);
for (NSInteger i = 0; i <= 6; i++) {
x = radius * cos(M_PI / 3 * i - M_PI_2) + origin.x;
y = radius * sin(M_PI / 3 * i - M_PI_2) + origin.y;
if (i == 0) {
[path moveToPoint:CGPointMake(x, y)];
} else {
[path addLineToPoint:CGPointMake(x, y)];
}
}
[path closePath];
path.lineWidth = lineWidth;
path.lineJoinStyle = kCGLineJoinMiter;
[color setStroke];
[path stroke];
//
CGContextSaveGState(ctx);
[path addClip];
CGContextDrawImage(ctx, rect, image.CGImage);
CGContextRestoreGState(ctx);
//
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}