UIBezierPath+CAShapeLayer绘制微信聊天图片效果

1,085 阅读1分钟
CGRect rect = CGRectMake(100, 100, 150, 250);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = [UIImage imageNamed:@"brand_launch"];
    
    [self.view addSubview:imageView];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    //起点
    [path moveToPoint:CGPointMake(0, 15)];
    //路径点
    [path addLineToPoint:CGPointMake(15, 15)];
    //第一个圆角点 x=尖角宽+圆角半径 y=圆角半径
    [path addArcWithCenter:CGPointMake(15+5, 5) radius:5 startAngle:M_PI endAngle:1.5*M_PI clockwise:YES];
    //第二个圆角点 x=view宽-圆角半径 y=圆角半径
    [path addArcWithCenter:CGPointMake(150-5, 5) radius:5 startAngle:1.5*M_PI endAngle:2*M_PI clockwise:YES];
    //第三个圆角点 x=view宽-圆角半径 y=view高-圆角半径
    [path addArcWithCenter:CGPointMake(150-5, 250-5) radius:5 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    //第四个圆角点 x=尖角宽+圆角半径 y=view高-圆角半径
    [path addArcWithCenter:CGPointMake(15+5, 250-5) radius:5 startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(15, 30)];
    [path closePath];//闭合路径
    
    layer.path = path.CGPath;
    
    imageView.layer.mask = layer;

"思路: 如果想应用在多种图片格式上? "

我可能会考虑把这个圆角放到一个UIView上,
然后通过重写ViewLayoutSubviews方法来实时取到正确的view的宽高, 来实现绘制 

各人按需求封装一下吧