两种方式给UIImage添加六边形边框

第一种方式:

      注意:此种方式下,如果是滚动的列表就会出现卡顿的性能问题。代码如下:

- (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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值