最近项目中需求中增加了一个聊天图片剪切成不规则的要求,查看了一下相关资料。结果用iOS SDK自带的API就可以实现如下这个效果。
// 传进去一个UIView进行绘制,返回一个 CAShapeLayer 对象,然后把改对象设置为view.layer.mask属性。
UIImageView *tempView = [[UIImageView alloc] init];
CAShapeLayer *layer = [self createMaskLayerWithView:tempView];
tempView.layer.mask = layer;
- (CAShapeLayer *)createMaskLayerWithView:(UIView *)view{
CGFloat viewWidth = CGRectGetWidth(view.frame);
CGFloat viewHeight = CGRectGetHeight(view.frame);
CGFloat rightSpace = 5.0;
CGFloat topSpace = 7.0;
CGPoint point1 = CGPointMake(rightSpace, 0);
CGPoint point2 = CGPointMake(rightSpace, topSpace);
CGPoint point3 = CGPointMake(0, topSpace);
CGPoint point4 = CGPointMake(rightSpace , topSpace + 6.0);
CGPoint point5 = CGPointMake(rightSpace, viewHeight);
CGPoint point6 = CGPointMake(viewWidth, viewHeight);
CGPoint point7 = CGPointMake(viewWidth, 0);
//根据自己描绘的路径,绘制想要的形状。
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:point1];
[path addLineToPoint:point2];
[path addLineToPoint:point3];
[path addLineToPoint:point4];
[path addLineToPoint:point5];
[path addLineToPoint:point6];
[path addLineToPoint:point7];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
return layer;
}