LenzDataManager.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // LenzFileManager.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by lr on 2023/3/8.
  6. //
  7. #import "LenzDataManager.h"
  8. NSString *const LenFileDirectoryVideoPath = @"/Len_VideoStitch_Resources/";
  9. NSString *const LenFileDirectorySinglePath = @"/single/";
  10. NSString *const LenFileDirectoryContinuousPath = @"/continuous/";
  11. NSString *const LenFileDirectoryMoviePath = @"/movie/";
  12. NSString *const LenFileDirectoryPanoramaPath = @"/panorama/";
  13. NSString *const LenFileDirectoryAiPanoramaPath = @"/aiPanorama/";
  14. @implementation LenzDataManager
  15. + (NSString *)saveImageWith:(NSData *)data mode:(SDKCaptureModeIndex)mode {
  16. switch (mode) {
  17. case SDK_CAPTURE_MODE_CONTINUOUS: {
  18. NSString *path = [self continuousPath];
  19. if ([data writeToFile:path atomically:NO]) {
  20. return path;
  21. }
  22. }
  23. break;
  24. case SDK_CAPTURE_MODE_SINGLE: {
  25. NSString *path = [self singlePath];
  26. if ([data writeToFile:path atomically:NO]) {
  27. return path;
  28. }
  29. }
  30. break;
  31. case SDK_CAPTURE_MODE_PANORAMA:{
  32. NSString *path = [self panoramPath];
  33. if ([data writeToFile:path atomically:NO]) {
  34. return path;
  35. }
  36. }
  37. break;
  38. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA:{
  39. NSString *path = [self aiPanoramPath];
  40. if ([data writeToFile:path atomically:NO]) {
  41. return path;
  42. }
  43. }
  44. break;
  45. default:
  46. return nil;
  47. break;
  48. }
  49. return nil;
  50. }
  51. //+ (NSString *)saveVideoUrl:(NSURL *)url model:(SDKCaptureModeIndex)mode {
  52. // NSString *path = [self moviePath];
  53. ////
  54. // NSError *error = nil;
  55. // NSData *data = [NSData dataWithContentsOfFile:url.absoluteString];
  56. // BOOL success = [data writeToFile:path atomically:YES];
  57. // BOOL success1 = [[NSFileManager defaultManager] copyItemAtPath:url.absoluteString toPath:path error:&error];
  58. //// = [[NSFileManager defaultManager] moveItemAtPath:url.absoluteString toPath:path error:nil];
  59. // if (success) {
  60. // return path;
  61. // } else {
  62. // return nil;
  63. // }
  64. //}
  65. + (NSString *)singlePath {
  66. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
  67. NSString *key = [NSString stringWithFormat:@"%.0f", timeInterval];
  68. return [[LenzDataManager singleDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];
  69. }
  70. + (NSString *)continuousPath {
  71. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
  72. NSString *key = [NSString stringWithFormat:@"%.0f", timeInterval];
  73. return [[LenzDataManager continuousDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];
  74. }
  75. + (NSString *)panoramPath {
  76. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
  77. NSString *key = [NSString stringWithFormat:@"%.0f", timeInterval];
  78. return [[LenzDataManager panoramsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];
  79. }
  80. + (NSString *)aiPanoramPath {
  81. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
  82. NSString *key = [NSString stringWithFormat:@"%.0f", timeInterval];
  83. return [[LenzDataManager aiPanoramsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];
  84. }
  85. + (NSString *)moviePath {
  86. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
  87. NSString *key = [NSString stringWithFormat:@"%.0f", timeInterval];
  88. return [[LenzDataManager movieDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov",key]];
  89. }
  90. #pragma mark - Directory
  91. + (NSString *)creatDirectory {
  92. NSFileManager *fileManager = [NSFileManager defaultManager];
  93. NSString *path = [LenzDataManager rootDirectory];
  94. BOOL fileExisted = [fileManager fileExistsAtPath:path isDirectory:nil];
  95. if (!fileExisted) {
  96. NSError *error = nil;
  97. [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  98. if (error) {
  99. NSLog(@"create root directory fail");
  100. } else {
  101. [self creatALLDirectoryIfNeeded];
  102. return path;
  103. }
  104. } else {
  105. [self creatALLDirectoryIfNeeded];
  106. }
  107. return path;
  108. }
  109. + (void)creatALLDirectoryIfNeeded {
  110. [self createSingleDir];
  111. [self creatContinuousDir];
  112. [self createMovieDir];
  113. [self createPanoramsDir];
  114. [self createAiPanoramsDir];
  115. }
  116. + (NSString *)rootDirectory {
  117. return [[LenzDataManager documentDirectory] stringByAppendingString:LenFileDirectoryVideoPath];
  118. }
  119. + (NSString *)documentDirectory {
  120. NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  121. if ([documentDirectories count] > 0) {
  122. return [documentDirectories objectAtIndex:0];
  123. }
  124. return @"";
  125. }
  126. + (NSString *)singleDirectory {
  127. return [[LenzDataManager rootDirectory] stringByAppendingPathComponent:LenFileDirectorySinglePath];
  128. }
  129. + (void)createSingleDir {
  130. NSFileManager *fileManager = [NSFileManager defaultManager];
  131. BOOL fileExisted = [fileManager fileExistsAtPath:[LenzDataManager singleDirectory] isDirectory:nil];
  132. if (!fileExisted) {
  133. NSError *error = nil;
  134. [fileManager createDirectoryAtPath:[LenzDataManager singleDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
  135. if (error) {
  136. NSLog(@"创建single directory失败");
  137. } else {
  138. // fail try
  139. }
  140. }
  141. }
  142. + (NSString *)continuousDirectory {
  143. return [[LenzDataManager rootDirectory] stringByAppendingPathComponent:LenFileDirectoryContinuousPath];
  144. }
  145. + (void)creatContinuousDir {
  146. NSFileManager *fileManager = [NSFileManager defaultManager];
  147. BOOL fileExisted = [fileManager fileExistsAtPath:[LenzDataManager continuousDirectory] isDirectory:nil];
  148. if (!fileExisted) {
  149. NSError *error = nil;
  150. [fileManager createDirectoryAtPath:[LenzDataManager continuousDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
  151. if (error) {
  152. NSLog(@"创建single directory失败");
  153. } else {
  154. // fail try
  155. }
  156. }
  157. }
  158. + (NSString *)movieDirectory {
  159. return [[LenzDataManager rootDirectory] stringByAppendingPathComponent:LenFileDirectoryMoviePath];
  160. }
  161. + (void)createMovieDir {
  162. NSFileManager *fileManager = [NSFileManager defaultManager];
  163. BOOL fileExisted = [fileManager fileExistsAtPath:[LenzDataManager movieDirectory] isDirectory:nil];
  164. if (!fileExisted) {
  165. NSError *error = nil;
  166. [fileManager createDirectoryAtPath:[LenzDataManager movieDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
  167. if (error) {
  168. NSLog(@"创建single directory失败");
  169. } else {
  170. // fail try
  171. }
  172. }
  173. }
  174. + (NSString *)panoramsDirectory {
  175. return [[LenzDataManager rootDirectory] stringByAppendingPathComponent:LenFileDirectoryPanoramaPath];
  176. }
  177. + (void)createPanoramsDir {
  178. NSFileManager *fileManager = [NSFileManager defaultManager];
  179. BOOL fileExisted = [fileManager fileExistsAtPath:[LenzDataManager panoramsDirectory] isDirectory:nil];
  180. if (!fileExisted) {
  181. NSError *error = nil;
  182. [fileManager createDirectoryAtPath:[LenzDataManager panoramsDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
  183. if (error) {
  184. NSLog(@"创建single directory失败");
  185. } else {
  186. // fail try
  187. }
  188. }
  189. }
  190. + (NSString *)aiPanoramsDirectory {
  191. return [[LenzDataManager rootDirectory] stringByAppendingPathComponent:LenFileDirectoryAiPanoramaPath];
  192. }
  193. + (void)createAiPanoramsDir {
  194. NSFileManager *fileManager = [NSFileManager defaultManager];
  195. BOOL fileExisted = [fileManager fileExistsAtPath:[LenzDataManager aiPanoramsDirectory] isDirectory:nil];
  196. if (!fileExisted) {
  197. NSError *error = nil;
  198. [fileManager createDirectoryAtPath:[LenzDataManager aiPanoramsDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
  199. if (error) {
  200. NSLog(@"创建single directory失败");
  201. } else {
  202. // fail try
  203. }
  204. }
  205. }
  206. @end