OperationNodeVideoBase.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // OperationNodeVideoBase.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/25.
  6. //
  7. #import "OperationNodeVideoBase.h"
  8. #import "PCSBaseViewController.h"
  9. #import "PCSRoundButton.h"
  10. #import "TimerLabel.h"
  11. #import "PCSModeList.h"
  12. #import "PanoramaGuideView.h"
  13. #import "SVProgressHUD.h"
  14. @interface OperationNodeVideoBase()
  15. @property (nonatomic) dispatch_source_t stitchTimer;
  16. @end
  17. @implementation OperationNodeVideoBase
  18. - (instancetype)initWithController:(PCSBaseViewController *)controller{
  19. self = [super init];
  20. if(self){
  21. _controller = controller;
  22. _isRecording = NO;
  23. }
  24. return self;
  25. }
  26. #pragma mark - strategy
  27. - (void)discardPhotoResultButtonTouchUpInside{
  28. [self cleanOnCompleted:^{
  29. [self.controller dismissViewControllerAnimated:YES completion:^{}];
  30. }];
  31. }
  32. - (BOOL)start{
  33. // [self updateButtonStatusByCurrentRecordingState];
  34. return YES;
  35. }
  36. - (void)stop{
  37. self.isRecording = NO;
  38. [self updateButtonStatusByCurrentRecordingState];
  39. if (!NSThread.isMainThread) {
  40. dispatch_async(dispatch_get_main_queue(), ^{
  41. // self.controller.albumButton.hidden = NO;
  42. // self.controller.savePhotoButton.hidden = NO;
  43. // self.controller.discardPhotoButton.hidden = NO;
  44. // self.controller.switchButton.hidden = NO;
  45. // UIView* list = (UIView*)self.controller.modeList;
  46. // self.controller.panGesture.enabled = YES;
  47. // list.userInteractionEnabled = YES;
  48. // [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  49. //
  50. // self.controller.albumButton.alpha = 1;
  51. // self.controller.savePhotoButton.alpha = 1;
  52. // self.controller.discardPhotoButton.alpha = 1;
  53. // self.controller.switchButton.alpha = 1;
  54. // } completion:^(BOOL finished) {}];
  55. });
  56. }
  57. }
  58. #pragma mark - button
  59. - (void)takePhotoButtonTouched{
  60. BOOL updatedRecording = !self.isRecording;
  61. self.controller.isVideoStitchStopedWhenRecording = NO;
  62. self.isRecording = updatedRecording && [self start];
  63. self.controller.panGesture.enabled = !self.isRecording;
  64. self.controller.timerLabel.backgroundColor = !self.isRecording ? [UIColor clearColor] : [PCSThemeColorManager orange];
  65. [self updateTakePhotoButtonStatus];
  66. // UIView* list = (UIView*)self.controller.modeList;
  67. if(!self.isRecording){
  68. self.controller.isVideoStitchStopedWhenRecording = YES;
  69. [self updateButtonStatusByCurrentRecordingState];
  70. if (self.modeIndex == SDK_CAPTURE_MODE_PANORAMA || self.modeIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  71. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  72. [self stop];
  73. });
  74. } else {
  75. [self stop];
  76. }
  77. }
  78. else{
  79. if (self.modeIndex == SDK_CAPTURE_MODE_PANORAMA || self.modeIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  80. self.stitchTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
  81. // dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)1 * NSEC_PER_SEC);
  82. uint64_t duration = (uint64_t)(1.0 * NSEC_PER_SEC);
  83. __block NSInteger count = 0;
  84. dispatch_source_set_timer(self.stitchTimer, DISPATCH_TIME_NOW, duration, 0 * NSEC_PER_SEC);
  85. dispatch_source_set_event_handler(self.stitchTimer, ^{
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. if (!self.isRecording) {
  88. dispatch_source_cancel(self.stitchTimer);
  89. self.controller.timerLabel.hidden = YES;
  90. self.controller.modeLabel.hidden = NO;
  91. return;
  92. }
  93. if (count <= self.recTime) {
  94. self.controller.timerLabel.backgroundColor = [PCSThemeColorManager orange];
  95. [self.controller.timerLabel updateWith:count];
  96. self.controller.timerLabel.hidden = NO;
  97. self.controller.modeLabel.hidden = YES;
  98. self.controller.flashButton.hidden = YES;
  99. self.controller.ai_fullView.hidden = YES;
  100. self.controller.panoramOrientationGuideView.hidden = YES;
  101. count++;
  102. } else {
  103. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"超过最大拼接时长,拼接结束"];
  104. self.controller.timerLabel.backgroundColor = [UIColor clearColor];
  105. [self.controller.timerLabel updateWith:0];
  106. self.controller.timerLabel.hidden = YES;
  107. self.controller.modeLabel.hidden = NO;
  108. self.controller.flashButton.hidden = NO;
  109. self.controller.ai_fullView.hidden = NO;
  110. self.controller.panoramOrientationGuideView.hidden = NO;
  111. self.controller.isVideoStitchStopedWhenRecording = YES;
  112. UIImage* img = self.isRecording ? [UIImage loadNamed:@"btn-stop-recording"] : [UIImage loadNamed:@"take-photo-btn"];
  113. [self.controller.takePhotoButton setImage:img forState:UIControlStateNormal];
  114. dispatch_source_cancel(self.stitchTimer);
  115. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  116. [self stop];
  117. [self.controller stopVideoDataOutputing];
  118. });
  119. [SVProgressHUD dismissWithDelay:3 completion:^{
  120. }];
  121. }
  122. });
  123. });
  124. dispatch_resume(self.stitchTimer);
  125. }
  126. [self updateUI];
  127. }
  128. }
  129. - (void)updateUI{
  130. }
  131. #pragma mark - update UI
  132. - (void)updateButtonStatusByCurrentRecordingState{
  133. // if (!NSThread.isMainThread) {
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. // self.controller.panGesture.enabled = !self.isRecording;
  136. // self.controller.switchButton.enabled = !self.isRecording;
  137. // self.controller.savePhotoButton.enabled = !self.isRecording;
  138. // self.controller.discardPhotoButton.enabled = !self.isRecording;
  139. // self.controller.backButton.enabled = !self.isRecording;
  140. // self.controller.albumButton.enabled = !self.isRecording;
  141. self.controller.timerLabel.hidden = YES;
  142. self.controller.modeLabel.hidden = NO;
  143. // self.controller.numberOfImagesLabel.hidden = NO;
  144. });
  145. // }
  146. }
  147. #pragma mark - Accessor
  148. //- (void)setIsRecording:(BOOL)isRecording{
  149. // _isRecording = isRecording;
  150. // UIImage* img = isRecording ? [UIImage loadNamed:@"btn-stop-recording"] : [UIImage loadNamed:@"take-photo-btn"];
  151. // if (!NSThread.isMainThread) {
  152. // dispatch_async(dispatch_get_main_queue(), ^{
  153. //
  154. // [self.controller.takePhotoButton setImage:img forState:UIControlStateNormal];
  155. // });
  156. // }
  157. //}
  158. - (void)updateTakePhotoButtonStatus {
  159. UIImage* img = self.isRecording ? [UIImage loadNamed:@"btn-stop-recording"] : [UIImage loadNamed:@"take-photo-btn"];
  160. if (!NSThread.isMainThread) {
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. [self.controller.takePhotoButton setImage:img forState:UIControlStateNormal];
  163. });
  164. } else {
  165. [self.controller.takePhotoButton setImage:img forState:UIControlStateNormal];
  166. }
  167. if (!self.isRecording) {
  168. [self.controller.timerLabel updateWith:0];
  169. self.controller.flashButton.hidden = NO;
  170. self.controller.discardPhotoButton.hidden = NO;
  171. self.controller.savePhotoButton.hidden = NO;
  172. if (self.modeIndex == SDK_CAPTURE_MODE_PANORAMA || self.modeIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  173. self.controller.switchButton.hidden = YES;
  174. } else {
  175. self.controller.switchButton.hidden = NO;
  176. }
  177. self.controller.modeList.hidden = NO;
  178. self.controller.ablumImageView.hidden = NO;
  179. // self.controller.numberOfImagesLabel.hidden = NO;
  180. } else {
  181. self.controller.flashButton.hidden = YES;
  182. self.controller.discardPhotoButton.hidden = YES;
  183. self.controller.savePhotoButton.hidden = YES;
  184. self.controller.switchButton.hidden = YES;
  185. self.controller.backButton.hidden = YES;
  186. self.controller.modeList.hidden = YES;
  187. self.controller.ablumImageView.hidden = YES;
  188. self.controller.numberOfImagesLabel.hidden = YES;
  189. }
  190. }
  191. @end