OperationNodeMovie.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. self.controller.modeLabel.hidden = YES;
  68. self.controller.timerLabel.backgroundColor = !self.isRecording ? [UIColor clearColor] : [PCSThemeColorManager orange];
  69. self.controller.timerLabel.hidden = NO;
  70. [self.controller.timerLabel updateWith:0];
  71. self.controller.timerLabel.padding = UIEdgeInsetsMake(0, 2.5, 0, 2.5);
  72. }
  73. - (void)discardPhotoResultButtonTouchUpInside{
  74. [QuitMultipleModeAlertViewController show:self.controller title:@"确认提醒" text:@"返回会清空已拍摄图片,请确认!" leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  75. [alertController dismissViewControllerAnimated:NO completion:^{}];
  76. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  77. [alertController dismissViewControllerAnimated:NO completion:^{
  78. [self cleanOnCompleted:^{
  79. [self.controller dismissViewControllerAnimated:YES completion:^{}];
  80. }];
  81. }];
  82. }];
  83. }
  84. - (void)triggered {
  85. if(self.controller.recordedFileUrl == nil){
  86. return;
  87. }
  88. NSURL* outputFileURL = self.controller.recordedFileUrl;
  89. NSURL* moviesDir = [PCSTools shared].moviesDir;
  90. NSFileManager* fileMgr = [NSFileManager defaultManager];
  91. BOOL isDir = YES;
  92. if(![fileMgr fileExistsAtPath:moviesDir.path isDirectory:&isDir]){
  93. NSError* err = nil;
  94. [fileMgr createDirectoryAtURL:moviesDir withIntermediateDirectories:YES attributes:nil error:&err];
  95. if(err != nil){
  96. return;
  97. }
  98. }
  99. NSError* err = nil;
  100. NSString* movieName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"mov"];
  101. NSURL* desUrl = [moviesDir URLByAppendingPathComponent: movieName isDirectory:NO];
  102. if([[LenzDBManager shared] insertWithName:movieName type:SDKCameraCapturedResourceIndexMovie] &&
  103. [fileMgr moveItemAtURL:outputFileURL toURL:desUrl error:&err]){
  104. AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:desUrl options:nil];
  105. AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  106. NSError *err = NULL;
  107. CMTime time = CMTimeMake(1, 65);
  108. CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&err];
  109. UIImage *frameImage= [[UIImage alloc] initWithCGImage:refImg scale:1.0 orientation:self.controller.thumbnailOritaionByCurrentVideoOrientation];
  110. [self.recordedMovies addObject:[[RecordedMovieModel alloc] initWithFileUrl:desUrl thumbnail:frameImage]];
  111. [self.controller.totalArray addObject:frameImage];
  112. self.isRecording = NO;
  113. [self updateUI];
  114. [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  115. self.controller.switchButton.alpha = 1;
  116. } completion:^(BOOL finished) {}];
  117. }
  118. else{
  119. NSLog(@"disk is full");
  120. }
  121. }
  122. - (SDKCaptureModeIndex)modeIndex{
  123. return SDK_CAPTURE_MODE_MOVIE;
  124. }
  125. - (void)willQuitOnCompletion: (WillQuitCompletionBlockType)block{
  126. showAlertToDiscardCachesWithTitle(self, @"", @"", block);
  127. }
  128. - (void)cleanOnCompleted:(void (^)(void))block{
  129. NSMutableArray<RecordedMovieModel*>* recordedMovies = self.recordedMovies;
  130. NSFileManager* fileMgr = [NSFileManager defaultManager];
  131. [recordedMovies enumerateObjectsUsingBlock:^(RecordedMovieModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  132. BOOL isDir = NO;
  133. if([fileMgr fileExistsAtPath:obj.fileUrl.path isDirectory: &isDir]){
  134. NSError* err = nil;
  135. if([fileMgr removeItemAtURL:obj.fileUrl error:&err]){
  136. NSString* name = [obj.fileUrl lastPathComponent];
  137. [[LenzDBManager shared] deleteByName: name type:SDKCameraCapturedResourceIndexMovie];
  138. }
  139. NSLog(@"%@", err);
  140. }
  141. }];
  142. [self.recordedMovies removeAllObjects];
  143. block();
  144. }
  145. - (BOOL)start{
  146. [super start];
  147. BOOL ret = [self.controller startRecording];
  148. return ret;
  149. }
  150. - (void)stop{
  151. self.isRecording = NO;
  152. [self.controller stopRecording];
  153. }
  154. - (BOOL)enable{
  155. return self.recordedMovies.count < self.maximumRecording;
  156. }
  157. - (NSString *)modeTitleString{
  158. return LOCALIZATION_STRING_KEY_MODE_TITLE_MOVIE;
  159. }
  160. - (NSString *)modeItemString{
  161. return @"视频";
  162. }
  163. - (NSString *)segueForAlbum{
  164. return @"show-movies";
  165. }
  166. - (UIImage *)firstCoverImage{
  167. return self.recordedMovies.firstObject.thumbnail;
  168. }
  169. - (void)deleteCacheForKey:(NSString *)key completion:(void (^)(BOOL))completion{
  170. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  171. NSString* path = key;
  172. __block RecordedMovieModel* model = nil;
  173. [self.recordedMovies enumerateObjectsUsingBlock:^(RecordedMovieModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  174. if([obj.fileUrl.path isEqualToString:path]){
  175. *stop = YES;
  176. model = obj;
  177. }
  178. }];
  179. if(model == nil){
  180. return;
  181. }
  182. NSFileManager* fileMgr = [NSFileManager defaultManager];
  183. NSError* err = nil;
  184. if([fileMgr fileExistsAtPath:path]){
  185. if([fileMgr removeItemAtPath:path error:&err]){
  186. NSString* name = [model.fileUrl lastPathComponent];
  187. [[LenzDBManager shared] deleteByName:name type:SDKCameraCapturedResourceIndexMovie];
  188. [self.recordedMovies removeObject:model];
  189. }
  190. else{
  191. NSLog(@"%@", err);
  192. }
  193. }
  194. completion(err == nil);
  195. });
  196. }
  197. #pragma mark - background
  198. - (void)willEnterBackground:(NSNotification *)noti{
  199. self.bgTaskToken = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  200. [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskToken];
  201. self.bgTaskToken = UIBackgroundTaskInvalid;
  202. }];
  203. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  204. [self.controller stopRecording];
  205. [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskToken];
  206. self.bgTaskToken = UIBackgroundTaskInvalid;
  207. });
  208. }
  209. @end