weChat聊天发送图片带有小尖角的实现

weChat聊天发送图片带有小尖角的实现

1.#import <UIKit/UIKit.h>
2.
3.@interface JKShapeImage : UIView
4.
5.@property (nonatomic,strong) UIImage *image;
6.
7.@property (nonatomic,getter=isOnLeft,assign) BOOL onLeft;
8.
9.@end
1.#import "JKShapeImage.h"
2.
3.@interface JKShapeImage ()
4.{
5. CAShapeLayer *maskLayer;
6. CALayer *contentLayer;
7.}
8.@end
9.
10.@implementation JKShapeImage
11.
12.-(instancetype)initWithFrame:(CGRect)frame{
13. if (self = [super initWithFrame:frame]) {
14. [self setShapeMask];
15. }
16. return self;
17.}
18.
19.-(void)setFrame:(CGRect)frame{
20. [super setFrame:frame];
21.}
22.
23.-(void)setShapeMask{
24. maskLayer = [CAShapeLayer layer];
25. maskLayer = [CAShapeLayer layer];
26. maskLayer.fillColor = [UIColor blackColor].CGColor;
27. maskLayer.strokeColor = [UIColor clearColor].CGColor;
28. maskLayer.frame = self.bounds;
29. maskLayer.contentsCenter = CGRectMake(0.65, 0.65, 0.2, 0.2);
30. maskLayer.contentsScale = [UIScreen mainScreen].scale; //非常关键设置自动拉伸的效果且不变形
31.
32. CGFloat w = self.bounds.size.width;
33. CGFloat h = self.bounds.size.height;
34.
35. UIBezierPath *path = [UIBezierPath bezierPath];
36. if (self.isOnLeft) {
37. [path moveToPoint:CGPointMake(9, 0)];
38. [path addLineToPoint:CGPointMake(w, 0)];
39. [path addLineToPoint:CGPointMake(w, h)];
40. [path addLineToPoint:CGPointMake(9, h)];
41. [path addLineToPoint:CGPointMake(9, 25)];
42. [path addLineToPoint:CGPointMake(0, 20)];
43. [path addLineToPoint:CGPointMake(9, 15)];
44. [path addLineToPoint:CGPointMake(9, 0)];
45. }else{
46. [path moveToPoint:CGPointMake(0, 0)];
47. [path addLineToPoint:CGPointMake(w - 9, 0)];
48. [path addLineToPoint:CGPointMake(w - 9, 15)];
49. [path addLineToPoint:CGPointMake(w, 20)];
50. [path addLineToPoint:CGPointMake(w - 9, 25)];
51. [path addLineToPoint:CGPointMake(w - 9, h)];
52. [path addLineToPoint:CGPointMake(0, h)];
53. [path addLineToPoint:CGPointMake(0, 0)];
54. }
55. maskLayer.path = path.CGPath;
56.
57.
58. contentLayer = [CALayer layer];
59. contentLayer.mask = maskLayer;
60. contentLayer.frame = self.bounds;
61. [self.layer addSublayer:contentLayer];
62.
63.}
64.
65.-(void)setImage:(UIImage *)image{
66. _image = image;
67. contentLayer.contents = (__bridge id)image.CGImage;
68.}
  • 实现效果
    Alt text
 
原文地址:https://www.cnblogs.com/buakaw/p/5282067.html