OperationNodeMovie.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // OperationNodeMovie.m
  3. // sampleSDK
  4. //
  5. // Created by 王昭威 on 2023/1/19.
  6. //
  7. #import "OperationNodeMovie.h"
  8. #import "PCSBaseViewController.h"
  9. #import "PCSRoundButton.h"
  10. #import <Photos/Photos.h>
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12. #import "QuitMultipleModeAlertViewController.h"
  13. #import "TimerLabel.h"
  14. #import "DBManager.h"
  15. #import "LenzSDKConstant.h"
  16. @implementation RecordedMovieModel
  17. - (instancetype)initWithFileUrl:(NSURL *)fileUrl thumbnail:(UIImage *)thumbnail{
  18. self = [super init];
  19. if(self){
  20. _fileUrl = fileUrl;
  21. _thumbnail = thumbnail;
  22. }
  23. return self;
  24. }
  25. @end
  26. @interface OperationNodeMovie ()
  27. @property (nonatomic, assign) BOOL isRecording;
  28. @property (nonatomic, assign) NSUInteger maximumRecording;
  29. @property (nonatomic, strong) NSMutableArray<RecordedMovieModel*>* recordedMovies;
  30. @property (nonatomic, assign, readonly) BOOL hasCache;
  31. @property (nonatomic, strong) UIImage* lastImage;
  32. @property (nonatomic, assign) UIBackgroundTaskIdentifier bgTaskToken;
  33. @end
  34. @implementation OperationNodeMovie
  35. @dynamic isRecording;
  36. - (NSArray<RecordedMovieModel *> *)cachedMovies{
  37. return [[NSArray alloc] initWithArray:self.recordedMovies];
  38. }
  39. - (UIImage *)lastImage{
  40. if (self.controller.totalArray.count > 0) {
  41. return [self.controller.totalArray lastObject];
  42. }else{
  43. return nil;
  44. }
  45. }
  46. - (BOOL)hasCache{
  47. return self.controller.totalArray.count > 0;
  48. }
  49. - (NSUInteger)numberOfCaches{
  50. return [self.recordedMovies count];
  51. }
  52. - (instancetype)initWithController:(PCSBaseViewController *)controller {
  53. self = [super initWithController:controller];
  54. if(self){
  55. _maximumRecording = 3;
  56. _recordedMovies = [[NSMutableArray alloc] init];
  57. }
  58. return self;
  59. }
  60. - (void)save {
  61. }
  62. - (void)didQuit{
  63. self.controller.modeLabel.hidden = NO;
  64. self.controller.timerLabel.hidden = YES;
  65. }
  66. - (void)updateUI{
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. self.controller.modeLabel.hidden = YES;
  69. self.controller.timerLabel.backgroundColor = !self.isRecording ? [UIColor clearColor] : [PCSThemeColorManager orange];
  70. self.controller.timerLabel.hidden = NO;
  71. [self.controller.timerLabel updateWith:0];
  72. self.controller.timerLabel.padding = UIEdgeInsetsMake(0, 2.5, 0, 2.5);
  73. });
  74. }
  75. - (void)discardPhotoResultButtonTouchUpInside{
  76. [QuitMultipleModeAlertViewController show:self.controller title:@"确认提醒" text:@"返回会清空已拍摄图片,请确认!" leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  77. [alertController dismissViewControllerAnimated:NO completion:^{}];
  78. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  79. [alertController dismissViewControllerAnimated:NO completion:^{
  80. [self cleanOnCompleted:^{
  81. [self.controller dismissViewControllerAnimated:YES completion:^{}];
  82. }];
  83. }];
  84. }];
  85. }
  86. - (void)triggered {
  87. if(self.controller.recordedFileUrl == nil){
  88. return;
  89. }
  90. NSURL* outputFileURL = self.controller.recordedFileUrl;
  91. NSURL* moviesDir = [PCSTools shared].moviesDir;
  92. NSFileManager* fileMgr = [NSFileManager defaultManager];
  93. BOOL isDir = YES;
  94. if(![fileMgr fileExistsAtPath:moviesDir.path isDirectory:&isDir]){
  95. NSError* err = nil;
  96. [fileMgr createDirectoryAtURL:moviesDir withIntermediateDirectories:YES attributes:nil error:&err];
  97. if(err != nil){
  98. return;
  99. }
  100. }
  101. NSError* err = nil;
  102. NSString* movieName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"mov"];
  103. NSURL* desUrl = [moviesDir URLByAppendingPathComponent: movieName isDirectory:NO];
  104. if([[LenzDBManager shared] insertWithName:movieName type:SDKCameraCapturedResourceIndexMovie] &&
  105. [fileMgr moveItemAtURL:outputFileURL toURL:desUrl error:&err]){
  106. AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:desUrl options:nil];
  107. AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  108. NSError *err = NULL;
  109. CMTime time = CMTimeMake(1, 65);
  110. CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&err];
  111. UIImage *frameImage= [[UIImage alloc] initWithCGImage:refImg scale:1.0 orientation:self.controller.thumbnailOritaionByCurrentVideoOrientation];
  112. [self.recordedMovies addObject:[[RecordedMovieModel alloc] initWithFileUrl:desUrl thumbnail:frameImage]];
  113. [self.controller.totalArray addObject:frameImage];
  114. self.isRecording = NO;
  115. [self updateUI];
  116. [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  117. self.controller.switchButton.alpha = 1;
  118. } completion:^(BOOL finished) {}];
  119. }
  120. else{
  121. NSLog(@"disk is full");
  122. }
  123. }
  124. - (SDKCaptureModeIndex)modeIndex{
  125. return SDK_CAPTURE_MODE_MOVIE;
  126. }
  127. - (void)willQuitOnCompletion: (WillQuitCompletionBlockType)block{
  128. showAlertToDiscardCachesWithTitle(self, @"", @"", block);
  129. }
  130. - (void)cleanOnCompleted:(void (^)(void))block{
  131. NSMutableArray<RecordedMovieModel*>* recordedMovies = self.recordedMovies;
  132. NSFileManager* fileMgr = [NSFileManager defaultManager];
  133. [recordedMovies enumerateObjectsUsingBlock:^(RecordedMovieModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  134. BOOL isDir = NO;
  135. if([fileMgr fileExistsAtPath:obj.fileUrl.path isDirectory: &isDir]){
  136. NSError* err = nil;
  137. if([fileMgr removeItemAtURL:obj.fileUrl error:&err]){
  138. NSString* name = [obj.fileUrl lastPathComponent];
  139. [[LenzDBManager shared] deleteByName: name type:SDKCameraCapturedResourceIndexMovie];
  140. }
  141. NSLog(@"%@", err);
  142. }
  143. }];
  144. [self.recordedMovies removeAllObjects];
  145. block();
  146. }
  147. - (BOOL)start{
  148. [super start];
  149. BOOL ret = [self.controller startRecording];
  150. return ret;
  151. }
  152. - (void)stop{
  153. self.isRecording = NO;
  154. [self.controller stopRecording];
  155. }
  156. - (BOOL)enable{
  157. return self.recordedMovies.count < self.maximumRecording;
  158. }
  159. - (NSString *)modeTitleString{
  160. return LOCALIZATION_STRING_KEY_MODE_TITLE_MOVIE;
  161. }
  162. - (NSString *)modeItemString{
  163. return @"视频";
  164. }
  165. - (NSString *)segueForAlbum{
  166. return @"show-movies";
  167. }
  168. - (UIImage *)firstCoverImage{
  169. return self.recordedMovies.firstObject.thumbnail;
  170. }
  171. - (void)deleteCacheForKey:(NSString *)key completion:(void (^)(BOOL))completion{
  172. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  173. NSString* path = key;
  174. __block RecordedMovieModel* model = nil;
  175. [self.recordedMovies enumerateObjectsUsingBlock:^(RecordedMovieModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  176. if([obj.fileUrl.path isEqualToString:path]){
  177. *stop = YES;
  178. model = obj;
  179. }
  180. }];
  181. if(model == nil){
  182. return;
  183. }
  184. NSFileManager* fileMgr = [NSFileManager defaultManager];
  185. NSError* err = nil;
  186. if([fileMgr fileExistsAtPath:path]){
  187. if([fileMgr removeItemAtPath:path error:&err]){
  188. NSString* name = [model.fileUrl lastPathComponent];
  189. [[LenzDBManager shared] deleteByName:name type:SDKCameraCapturedResourceIndexMovie];
  190. [self.recordedMovies removeObject:model];
  191. }
  192. else{
  193. NSLog(@"%@", err);
  194. }
  195. }
  196. completion(err == nil);
  197. });
  198. }
  199. #pragma mark - background
  200. - (void)willEnterBackground:(NSNotification *)noti{
  201. self.bgTaskToken = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  202. [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskToken];
  203. self.bgTaskToken = UIBackgroundTaskInvalid;
  204. }];
  205. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  206. [self.controller stopRecording];
  207. [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskToken];
  208. self.bgTaskToken = UIBackgroundTaskInvalid;
  209. });
  210. }
  211. @end