weChat聊天发送图片带有小尖角的实现
#import <UIKit/UIKit.h>
@interface JKShapeImage : UIView
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,getter=isOnLeft,assign) BOOL onLeft;
@end
#import "JKShapeImage.h"
@interface JKShapeImage ()
{
CAShapeLayer *maskLayer;
CALayer *contentLayer;
}
@end
@implementation JKShapeImage
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setShapeMask];
}
return self;
}
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
}
-(void)setShapeMask{
maskLayer = [CAShapeLayer layer];
maskLayer = [CAShapeLayer layer];
maskLayer.fillColor = [UIColor blackColor].CGColor;
maskLayer.strokeColor = [UIColor clearColor].CGColor;
maskLayer.frame = self.bounds;
maskLayer.contentsCenter = CGRectMake(0.65, 0.65, 0.2, 0.2);
maskLayer.contentsScale = [UIScreen mainScreen].scale;
CGFloat w = self.bounds.size.width;
CGFloat h = self.bounds.size.height;
UIBezierPath *path = [UIBezierPath bezierPath];
if (self.isOnLeft) {
[path moveToPoint:CGPointMake(9, 0)];
[path addLineToPoint:CGPointMake(w, 0)];
[path addLineToPoint:CGPointMake(w, h)];
[path addLineToPoint:CGPointMake(9, h)];
[path addLineToPoint:CGPointMake(9, 25)];
[path addLineToPoint:CGPointMake(0, 20)];
[path addLineToPoint:CGPointMake(9, 15)];
[path addLineToPoint:CGPointMake(9, 0)];
}else{
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(w - 9, 0)];
[path addLineToPoint:CGPointMake(w - 9, 15)];
[path addLineToPoint:CGPointMake(w, 20)];
[path addLineToPoint:CGPointMake(w - 9, 25)];
[path addLineToPoint:CGPointMake(w - 9, h)];
[path addLineToPoint:CGPointMake(0, h)];
[path addLineToPoint:CGPointMake(0, 0)];
}
maskLayer.path = path.CGPath;
contentLayer = [CALayer layer];
contentLayer.mask = maskLayer;
contentLayer.frame = self.bounds;
[self.layer addSublayer:contentLayer];
}
-(void)setImage:(UIImage *)image{
_image = image;
contentLayer.contents = (__bridge id)image.CGImage;
}
- 实现效果