HXPhotoEditChartletPreviewView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // HXPhotoEditChartletPreviewView.m
  3. // photoEditDemo
  4. //
  5. // Created by Silence on 2020/7/1.
  6. // Copyright © 2020 Silence. All rights reserved.
  7. //
  8. #import "HXPhotoEditChartletPreviewView.h"
  9. #import "UIView+HXExtension.h"
  10. #import "HXPhotoDefine.h"
  11. #import "HXPhotoEditChartletModel.h"
  12. #import "UIImage+HXExtension.h"
  13. #import "UIImageView+HXExtension.h"
  14. #import "NSBundle+HXPhotoPicker.h"
  15. @interface HXPhotoEditChartletPreviewView ()
  16. @property (strong, nonatomic) UIActivityIndicatorView *loadingView;
  17. @property (weak, nonatomic) IBOutlet UIView *contentView;
  18. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  19. @property (assign, nonatomic) CGSize imageSize;
  20. @property (assign, nonatomic) CGRect viewFrame;
  21. @property (assign, nonatomic) CGPoint point;
  22. @property (assign, nonatomic) CGFloat triangleX;
  23. @property (strong, nonatomic) HXPhotoEditChartletModel *model;
  24. @end
  25. @implementation HXPhotoEditChartletPreviewView
  26. + (instancetype)showPreviewWithModel:(HXPhotoEditChartletModel *)model atPoint:(CGPoint)point {
  27. HXPhotoEditChartletPreviewView *view = [self initView];
  28. view.point = point;
  29. view.model = model;
  30. return view;
  31. }
  32. + (instancetype)initView {
  33. return [[[NSBundle hx_photoPickerBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
  34. }
  35. - (void)drawRect:(CGRect)rect {
  36. [super drawRect:rect];
  37. CGContextRef context = UIGraphicsGetCurrentContext();
  38. CGContextBeginPath(context);
  39. CGContextMoveToPoint (context, self.triangleX - 15, self.hx_h - 20);
  40. CGContextAddLineToPoint(context, self.triangleX + 15, self.hx_h - 20);
  41. CGContextAddLineToPoint(context, self.triangleX, self.hx_h);
  42. CGContextClosePath(context);
  43. CGContextSetRGBFillColor(context, 1, 1, 1, 1);
  44. CGContextFillPath(context);
  45. }
  46. - (void)setModel:(HXPhotoEditChartletModel *)model {
  47. _model = model;
  48. if (model.type == HXPhotoEditChartletModelType_Image) {
  49. self.imageView.image = model.image;
  50. self.imageSize = self.imageView.image.size;
  51. [self updateFrame];
  52. }else if (model.type == HXPhotoEditChartletModelType_ImageNamed) {
  53. UIImage *image = [UIImage hx_imageContentsOfFile:model.imageNamed];
  54. self.imageView.image = image;
  55. self.imageSize = self.imageView.image.size;
  56. [self updateFrame];
  57. }else if (model.type == HXPhotoEditChartletModelType_NetworkURL) {
  58. self.loadingView.hidden = NO;
  59. [self.loadingView startAnimating];
  60. self.imageSize = CGSizeMake(HX_ScreenWidth / 3, HX_ScreenWidth / 3);
  61. [self updateFrame];
  62. [self layoutIfNeeded];
  63. HXWeakSelf
  64. [self.imageView hx_setImageWithURL:model.networkURL progress:^(CGFloat progress) {
  65. if (progress < 1) {
  66. weakSelf.loadingView.hidden = NO;
  67. [weakSelf.loadingView startAnimating];
  68. }
  69. } completed:^(UIImage *image, NSError *error) {
  70. weakSelf.loadingView.hidden = YES;
  71. [weakSelf.loadingView stopAnimating];
  72. weakSelf.viewFrame = CGRectZero;
  73. weakSelf.imageSize = weakSelf.imageView.image.size;
  74. [UIView animateWithDuration:0.25 animations:^{
  75. [weakSelf updateFrame];
  76. [weakSelf layoutIfNeeded];
  77. [weakSelf setNeedsDisplay];
  78. }];
  79. }];
  80. }
  81. }
  82. - (void)updateFrame {
  83. self.frame = self.viewFrame;
  84. self.loadingView.center = CGPointMake(self.hx_w / 2, self.hx_h / 2 - 5);
  85. }
  86. - (CGRect)viewFrame {
  87. if (CGRectIsEmpty(_viewFrame)) {
  88. CGFloat width = HX_ScreenWidth / 2;
  89. CGFloat height = width;
  90. CGFloat imgWidth = self.imageSize.width;
  91. CGFloat imgHeight = self.imageSize.height;
  92. if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown || HX_UI_IS_IPAD) {
  93. if (imgWidth > imgHeight) {
  94. width = HX_ScreenWidth - 40;
  95. }else if (imgHeight > imgWidth) {
  96. height = HX_ScreenWidth - 40;
  97. }
  98. }else {
  99. width = HX_ScreenHeight / 2;
  100. height = width;
  101. if (imgWidth > imgHeight) {
  102. width = HX_ScreenHeight - 40;
  103. }else if (imgHeight > imgWidth) {
  104. height = HX_ScreenHeight - 40;
  105. }
  106. }
  107. CGFloat w;
  108. CGFloat h;
  109. if (imgWidth > width) {
  110. imgHeight = width / imgWidth * imgHeight;
  111. }
  112. if (imgHeight > height) {
  113. w = height / self.imageSize.height * imgWidth;
  114. h = height;
  115. }else {
  116. if (imgWidth > width) {
  117. w = width;
  118. }else {
  119. w = imgWidth;
  120. }
  121. h = imgHeight;
  122. }
  123. w += 10;
  124. h += 20;
  125. CGFloat x = self.point.x - w / 2;
  126. CGFloat y = self.point.y - 10 - h;
  127. if (x + w > HX_ScreenWidth - 15) {
  128. x = HX_ScreenWidth - 15 - w;
  129. }
  130. if (x < 15) {
  131. x = 15;
  132. }
  133. self.triangleX = self.point.x - x;
  134. _viewFrame = CGRectMake(x, y, w, h);
  135. }
  136. return _viewFrame;
  137. }
  138. - (void)awakeFromNib {
  139. [super awakeFromNib];
  140. [self addSubview:self.loadingView];
  141. if (HX_IOS11_Later) {
  142. [self.contentView hx_radiusWithRadius:5 corner:UIRectCornerAllCorners];
  143. }
  144. self.layer.shadowOffset = CGSizeMake(0, 0);
  145. self.layer.shadowColor = [UIColor blackColor].CGColor;
  146. self.layer.shadowRadius = 5.f;
  147. self.layer.shadowOpacity = 0.3f;
  148. _viewFrame = CGRectZero;
  149. }
  150. - (void)layoutSubviews {
  151. [super layoutSubviews];
  152. if (HX_IOS11_Earlier) {
  153. [self.contentView hx_radiusWithRadius:5 corner:UIRectCornerAllCorners];
  154. }
  155. }
  156. - (UIActivityIndicatorView *)loadingView {
  157. if (!_loadingView) {
  158. _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  159. _loadingView.hidden = YES;
  160. }
  161. return _loadingView;
  162. }
  163. @end