Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeGallery.mm 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. #import <Foundation/Foundation.h>
  2. #import <Photos/Photos.h>
  3. #import <MobileCoreServices/UTCoreTypes.h>
  4. #import <MobileCoreServices/MobileCoreServices.h>
  5. #import <ImageIO/ImageIO.h>
  6. #import <PhotosUI/PhotosUI.h>
  7. extern UIViewController* UnityGetGLViewController();
  8. #define CHECK_IOS_VERSION( version ) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedAscending)
  9. @interface UNativeGallery:NSObject
  10. + (int)checkPermission:(BOOL)readPermission permissionFreeMode:(BOOL)permissionFreeMode;
  11. + (void)requestPermission:(BOOL)readPermission permissionFreeMode:(BOOL)permissionFreeMode;
  12. + (void)showLimitedLibraryPicker;
  13. + (void)openSettings;
  14. + (int)canPickMultipleMedia;
  15. + (void)saveMedia:(NSString *)path albumName:(NSString *)album isImage:(BOOL)isImage permissionFreeMode:(BOOL)permissionFreeMode;
  16. + (void)pickMedia:(int)mediaType savePath:(NSString *)mediaSavePath permissionFreeMode:(BOOL)permissionFreeMode selectionLimit:(int)selectionLimit;
  17. + (int)isMediaPickerBusy;
  18. + (int)getMediaTypeFromExtension:(NSString *)extension;
  19. + (char *)getImageProperties:(NSString *)path;
  20. + (char *)getVideoProperties:(NSString *)path;
  21. + (char *)getVideoThumbnail:(NSString *)path savePath:(NSString *)savePath maximumSize:(int)maximumSize captureTime:(double)captureTime;
  22. + (char *)loadImageAtPath:(NSString *)path tempFilePath:(NSString *)tempFilePath maximumSize:(int)maximumSize;
  23. @end
  24. @implementation UNativeGallery
  25. static NSString *pickedMediaSavePath;
  26. static UIImagePickerController *imagePicker;
  27. API_AVAILABLE(ios(14))
  28. static PHPickerViewController *imagePickerNew;
  29. static int imagePickerState = 0; // 0 -> none, 1 -> showing (always in this state on iPad), 2 -> finished
  30. static BOOL simpleMediaPickMode;
  31. static BOOL pickingMultipleFiles = NO;
  32. + (int)checkPermission:(BOOL)readPermission permissionFreeMode:(BOOL)permissionFreeMode
  33. {
  34. // On iOS 11 and later, permission isn't mandatory to fetch media from Photos
  35. if( readPermission && permissionFreeMode && CHECK_IOS_VERSION( @"11.0" ) )
  36. return 1;
  37. // Photos permissions has changed on iOS 14
  38. if( @available(iOS 14.0, *) )
  39. {
  40. // Request ReadWrite permission in 2 cases:
  41. // 1) When attempting to pick media from Photos with PHPhotoLibrary (readPermission=true and permissionFreeMode=false)
  42. // 2) When attempting to write media to a specific album in Photos using PHPhotoLibrary (readPermission=false and permissionFreeMode=false)
  43. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:( ( readPermission || !permissionFreeMode ) ? PHAccessLevelReadWrite : PHAccessLevelAddOnly )];
  44. if( status == PHAuthorizationStatusAuthorized )
  45. return 1;
  46. else if( status == PHAuthorizationStatusRestricted )
  47. return 3;
  48. else if( status == PHAuthorizationStatusNotDetermined )
  49. return 2;
  50. else
  51. return 0;
  52. }
  53. else
  54. {
  55. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  56. if( status == PHAuthorizationStatusAuthorized )
  57. return 1;
  58. else if( status == PHAuthorizationStatusNotDetermined )
  59. return 2;
  60. else
  61. return 0;
  62. }
  63. }
  64. + (void)requestPermission:(BOOL)readPermission permissionFreeMode:(BOOL)permissionFreeMode
  65. {
  66. // On iOS 11 and later, permission isn't mandatory to fetch media from Photos
  67. if( readPermission && permissionFreeMode && CHECK_IOS_VERSION( @"11.0" ) )
  68. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "1" );
  69. else if( @available(iOS 14.0, *) )
  70. {
  71. // Photos permissions has changed on iOS 14. There are 2 permission dialogs now:
  72. // - AddOnly permission dialog: has 2 options: "Allow" and "Don't Allow". This dialog grants permission for save operations only. Unfortunately,
  73. // saving media to a custom album isn't possible with this dialog, media can only be saved to the default Photos album
  74. // - ReadWrite permission dialog: has 3 options: "Allow Access to All Photos" (i.e. full permission), "Select Photos" (i.e. limited access) and
  75. // "Don't Allow". To be able to save media to a custom album, user must grant Full Photos permission. Thus, even when readPermission is false,
  76. // this dialog will be used if PermissionFreeMode is set to false. So, PermissionFreeMode determines whether or not saving to a custom album is supported
  77. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:( readPermission ? PHAccessLevelReadWrite : PHAccessLevelAddOnly )];
  78. if( status == PHAuthorizationStatusAuthorized )
  79. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "1" );
  80. else if( status == PHAuthorizationStatusRestricted )
  81. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "3" );
  82. else if( status == PHAuthorizationStatusNotDetermined )
  83. {
  84. [PHPhotoLibrary requestAuthorizationForAccessLevel:( readPermission ? PHAccessLevelReadWrite : PHAccessLevelAddOnly ) handler:^( PHAuthorizationStatus status )
  85. {
  86. if( status == PHAuthorizationStatusAuthorized )
  87. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "1" );
  88. else if( status == PHAuthorizationStatusRestricted )
  89. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "3" );
  90. else
  91. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "0" );
  92. }];
  93. }
  94. else
  95. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "0" );
  96. }
  97. else
  98. {
  99. // Request permission using Photos framework: https://stackoverflow.com/a/32989022/2373034
  100. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  101. if( status == PHAuthorizationStatusAuthorized )
  102. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "1" );
  103. else if( status == PHAuthorizationStatusNotDetermined )
  104. {
  105. [PHPhotoLibrary requestAuthorization:^( PHAuthorizationStatus status )
  106. {
  107. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", ( status == PHAuthorizationStatusAuthorized ) ? "1" : "0" );
  108. }];
  109. }
  110. else
  111. UnitySendMessage( "NGPermissionCallbackiOS", "OnPermissionRequested", "0" );
  112. }
  113. }
  114. // When Photos permission is set to restricted, allows user to change the permission or change the list of restricted images
  115. // It doesn't support a deterministic callback; for example there is a photoLibraryDidChange event but it won't be invoked if
  116. // user doesn't change the list of restricted images
  117. + (void)showLimitedLibraryPicker
  118. {
  119. if( @available(iOS 14.0, *) )
  120. {
  121. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
  122. if( status == PHAuthorizationStatusNotDetermined )
  123. [self requestPermission:YES permissionFreeMode:NO];
  124. else if( status == PHAuthorizationStatusRestricted )
  125. [[PHPhotoLibrary sharedPhotoLibrary] presentLimitedLibraryPickerFromViewController:UnityGetGLViewController()];
  126. }
  127. }
  128. // Credit: https://stackoverflow.com/a/25453667/2373034
  129. + (void)openSettings
  130. {
  131. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  132. }
  133. + (int)canPickMultipleMedia
  134. {
  135. if( @available(iOS 14.0, *) )
  136. return 1;
  137. else
  138. return 0;
  139. }
  140. // Credit: https://stackoverflow.com/a/39909129/2373034
  141. + (void)saveMedia:(NSString *)path albumName:(NSString *)album isImage:(BOOL)isImage permissionFreeMode:(BOOL)permissionFreeMode
  142. {
  143. // On iOS 14+, permission workflow has changed significantly with the addition of PHAuthorizationStatusRestricted permission. On those versions,
  144. // user must grant Full Photos permission to be able to save to a custom album. Hence, there are 2 workflows:
  145. // - If PermissionFreeMode is enabled, save the media directly to the default album (i.e. ignore 'album' parameter). This will present a simple
  146. // permission dialog stating "The app requires access to Photos to save media to it." and the "Selected Photos" permission won't be listed in the options
  147. // - Otherwise, the more complex "The app requires access to Photos to interact with it." permission dialog will be shown and if the user grants
  148. // Full Photos permission, only then the image will be saved to the specified album. If user selects "Selected Photos" permission, default album will be
  149. // used as fallback
  150. void (^saveToPhotosAlbum)() = ^void()
  151. {
  152. if( isImage )
  153. {
  154. // Try preserving image metadata (essential for animated gif images)
  155. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:
  156. ^{
  157. [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:[NSURL fileURLWithPath:path]];
  158. }
  159. completionHandler:^( BOOL success, NSError *error )
  160. {
  161. if( success )
  162. {
  163. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  164. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveCompleted", "" );
  165. }
  166. else
  167. {
  168. NSLog( @"Error creating asset in default Photos album: %@", error );
  169. UIImage *image = [UIImage imageWithContentsOfFile:path];
  170. if( image != nil )
  171. UIImageWriteToSavedPhotosAlbum( image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge_retained void *) path );
  172. else
  173. {
  174. NSLog( @"Couldn't create UIImage from file at path: %@", path );
  175. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  176. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveFailed", "" );
  177. }
  178. }
  179. }];
  180. }
  181. else
  182. {
  183. if( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( path ) )
  184. UISaveVideoAtPathToSavedPhotosAlbum( path, self, @selector(video:didFinishSavingWithError:contextInfo:), (__bridge_retained void *) path );
  185. else
  186. {
  187. NSLog( @"Video at path isn't compatible with saved photos album: %@", path );
  188. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  189. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveFailed", "" );
  190. }
  191. }
  192. };
  193. void (^saveBlock)(PHAssetCollection *assetCollection) = ^void( PHAssetCollection *assetCollection )
  194. {
  195. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:
  196. ^{
  197. PHAssetChangeRequest *assetChangeRequest;
  198. if( isImage )
  199. assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:[NSURL fileURLWithPath:path]];
  200. else
  201. assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:path]];
  202. PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
  203. [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
  204. }
  205. completionHandler:^( BOOL success, NSError *error )
  206. {
  207. if( success )
  208. {
  209. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  210. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveCompleted", "" );
  211. }
  212. else
  213. {
  214. NSLog( @"Error creating asset: %@", error );
  215. saveToPhotosAlbum();
  216. }
  217. }];
  218. };
  219. if( permissionFreeMode && CHECK_IOS_VERSION( @"14.0" ) )
  220. saveToPhotosAlbum();
  221. else
  222. {
  223. PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
  224. fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", album];
  225. PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
  226. if( fetchResult.count > 0 )
  227. saveBlock( fetchResult.firstObject);
  228. else
  229. {
  230. __block PHObjectPlaceholder *albumPlaceholder;
  231. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:
  232. ^{
  233. PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album];
  234. albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
  235. }
  236. completionHandler:^( BOOL success, NSError *error )
  237. {
  238. if( success )
  239. {
  240. PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
  241. if( fetchResult.count > 0 )
  242. saveBlock( fetchResult.firstObject);
  243. else
  244. {
  245. NSLog( @"Error creating album: Album placeholder not found" );
  246. saveToPhotosAlbum();
  247. }
  248. }
  249. else
  250. {
  251. NSLog( @"Error creating album: %@", error );
  252. saveToPhotosAlbum();
  253. }
  254. }];
  255. }
  256. }
  257. }
  258. + (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  259. {
  260. NSString* path = (__bridge_transfer NSString *)(contextInfo);
  261. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  262. if( error == nil )
  263. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveCompleted", "" );
  264. else
  265. {
  266. NSLog( @"Error saving image with UIImageWriteToSavedPhotosAlbum: %@", error );
  267. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveFailed", "" );
  268. }
  269. }
  270. + (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  271. {
  272. NSString* path = (__bridge_transfer NSString *)(contextInfo);
  273. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  274. if( error == nil )
  275. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveCompleted", "" );
  276. else
  277. {
  278. NSLog( @"Error saving video with UISaveVideoAtPathToSavedPhotosAlbum: %@", error );
  279. UnitySendMessage( "NGMediaSaveCallbackiOS", "OnMediaSaveFailed", "" );
  280. }
  281. }
  282. // Credit: https://stackoverflow.com/a/10531752/2373034
  283. + (void)pickMedia:(int)mediaType savePath:(NSString *)mediaSavePath permissionFreeMode:(BOOL)permissionFreeMode selectionLimit:(int)selectionLimit
  284. {
  285. pickedMediaSavePath = mediaSavePath;
  286. imagePickerState = 1;
  287. simpleMediaPickMode = permissionFreeMode && CHECK_IOS_VERSION( @"11.0" );
  288. if( @available(iOS 14.0, *) )
  289. {
  290. // PHPickerViewController is used on iOS 14
  291. PHPickerConfiguration *config = simpleMediaPickMode ? [[PHPickerConfiguration alloc] init] : [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
  292. config.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeCurrent;
  293. config.selectionLimit = selectionLimit;
  294. pickingMultipleFiles = selectionLimit != 1;
  295. // mediaType is a bitmask:
  296. // 1: image
  297. // 2: video
  298. // 4: audio (not supported)
  299. if( mediaType == 1 )
  300. config.filter = [PHPickerFilter anyFilterMatchingSubfilters:[NSArray arrayWithObjects:[PHPickerFilter imagesFilter], [PHPickerFilter livePhotosFilter], nil]];
  301. else if( mediaType == 2 )
  302. config.filter = [PHPickerFilter videosFilter];
  303. else
  304. config.filter = [PHPickerFilter anyFilterMatchingSubfilters:[NSArray arrayWithObjects:[PHPickerFilter imagesFilter], [PHPickerFilter livePhotosFilter], [PHPickerFilter videosFilter], nil]];
  305. imagePickerNew = [[PHPickerViewController alloc] initWithConfiguration:config];
  306. imagePickerNew.delegate = (id) self;
  307. [UnityGetGLViewController() presentViewController:imagePickerNew animated:YES completion:^{ imagePickerState = 0; }];
  308. }
  309. else
  310. {
  311. // UIImagePickerController is used on previous versions
  312. imagePicker = [[UIImagePickerController alloc] init];
  313. imagePicker.delegate = (id) self;
  314. imagePicker.allowsEditing = NO;
  315. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  316. // mediaType is a bitmask:
  317. // 1: image
  318. // 2: video
  319. // 4: audio (not supported)
  320. if( mediaType == 1 )
  321. imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeLivePhoto, nil];
  322. else if( mediaType == 2 )
  323. imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, (NSString *)kUTTypeVideo, nil];
  324. else
  325. imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeLivePhoto, (NSString *)kUTTypeMovie, (NSString *)kUTTypeVideo, nil];
  326. if( mediaType != 1 )
  327. {
  328. // Don't compress picked videos if possible
  329. imagePicker.videoExportPreset = AVAssetExportPresetPassthrough;
  330. }
  331. UIViewController *rootViewController = UnityGetGLViewController();
  332. if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) // iPad
  333. {
  334. imagePicker.modalPresentationStyle = UIModalPresentationPopover;
  335. UIPopoverPresentationController *popover = imagePicker.popoverPresentationController;
  336. if( popover != nil )
  337. {
  338. popover.sourceView = rootViewController.view;
  339. popover.sourceRect = CGRectMake( rootViewController.view.frame.size.width / 2, rootViewController.view.frame.size.height / 2, 1, 1 );
  340. popover.permittedArrowDirections = 0;
  341. }
  342. }
  343. [rootViewController presentViewController:imagePicker animated:YES completion:^{ imagePickerState = 0; }];
  344. }
  345. }
  346. + (int)isMediaPickerBusy
  347. {
  348. if( imagePickerState == 2 )
  349. return 1;
  350. if( imagePicker != nil )
  351. {
  352. if( imagePickerState == 1 || [imagePicker presentingViewController] == UnityGetGLViewController() )
  353. return 1;
  354. else
  355. {
  356. imagePicker = nil;
  357. return 0;
  358. }
  359. }
  360. else if( @available(iOS 14.0, *) )
  361. {
  362. if( imagePickerNew == nil )
  363. return 0;
  364. else if( imagePickerState == 1 || [imagePickerNew presentingViewController] == UnityGetGLViewController() )
  365. return 1;
  366. else
  367. {
  368. imagePickerNew = nil;
  369. return 0;
  370. }
  371. }
  372. else
  373. return 0;
  374. }
  375. #pragma clang diagnostic push
  376. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  377. + (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  378. {
  379. NSString *resultPath = nil;
  380. if( [info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage] )
  381. {
  382. NSLog( @"Picked an image" );
  383. // Try to obtain the raw data of the image (which allows picking gifs properly or preserving metadata)
  384. PHAsset *asset = nil;
  385. // Try fetching the source image via UIImagePickerControllerImageURL
  386. NSURL *mediaUrl = info[UIImagePickerControllerImageURL];
  387. if( mediaUrl != nil )
  388. {
  389. NSString *imagePath = [mediaUrl path];
  390. if( imagePath != nil && [[NSFileManager defaultManager] fileExistsAtPath:imagePath] )
  391. {
  392. NSError *error;
  393. NSString *newPath = [pickedMediaSavePath stringByAppendingPathExtension:[imagePath pathExtension]];
  394. if( ![[NSFileManager defaultManager] fileExistsAtPath:newPath] || [[NSFileManager defaultManager] removeItemAtPath:newPath error:&error] )
  395. {
  396. if( [[NSFileManager defaultManager] copyItemAtPath:imagePath toPath:newPath error:&error] )
  397. {
  398. resultPath = newPath;
  399. NSLog( @"Copied source image from UIImagePickerControllerImageURL" );
  400. }
  401. else
  402. NSLog( @"Error copying image: %@", error );
  403. }
  404. else
  405. NSLog( @"Error deleting existing image: %@", error );
  406. }
  407. }
  408. if( resultPath == nil )
  409. asset = info[UIImagePickerControllerPHAsset];
  410. if( resultPath == nil && !simpleMediaPickMode )
  411. {
  412. if( asset == nil )
  413. {
  414. mediaUrl = info[UIImagePickerControllerReferenceURL] ?: info[UIImagePickerControllerMediaURL];
  415. if( mediaUrl != nil )
  416. asset = [[PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:mediaUrl] options:nil] firstObject];
  417. }
  418. resultPath = [self trySavePHAsset:asset atIndex:1];
  419. }
  420. if( resultPath == nil )
  421. {
  422. // Save image as PNG
  423. UIImage *image = info[UIImagePickerControllerOriginalImage];
  424. if( image != nil )
  425. {
  426. resultPath = [pickedMediaSavePath stringByAppendingPathExtension:@"png"];
  427. if( ![self saveImageAsPNG:image toPath:resultPath] )
  428. {
  429. NSLog( @"Error creating PNG image" );
  430. resultPath = nil;
  431. }
  432. }
  433. else
  434. NSLog( @"Error fetching original image from picker" );
  435. }
  436. }
  437. else if( [info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeLivePhoto] )
  438. {
  439. NSLog( @"Picked a live photo" );
  440. // Save live photo as PNG
  441. UIImage *image = info[UIImagePickerControllerOriginalImage];
  442. if( image != nil )
  443. {
  444. resultPath = [pickedMediaSavePath stringByAppendingPathExtension:@"png"];
  445. if( ![self saveImageAsPNG:image toPath:resultPath] )
  446. {
  447. NSLog( @"Error creating PNG image" );
  448. resultPath = nil;
  449. }
  450. }
  451. else
  452. NSLog( @"Error fetching live photo's still image from picker" );
  453. }
  454. else
  455. {
  456. NSLog( @"Picked a video" );
  457. NSURL *mediaUrl = info[UIImagePickerControllerMediaURL] ?: info[UIImagePickerControllerReferenceURL];
  458. if( mediaUrl != nil )
  459. {
  460. resultPath = [mediaUrl path];
  461. // On iOS 13, picked file becomes unreachable as soon as the UIImagePickerController disappears,
  462. // in that case, copy the video to a temporary location
  463. if( @available(iOS 13.0, *) )
  464. {
  465. NSError *error;
  466. NSString *newPath = [pickedMediaSavePath stringByAppendingPathExtension:[resultPath pathExtension]];
  467. if( ![[NSFileManager defaultManager] fileExistsAtPath:newPath] || [[NSFileManager defaultManager] removeItemAtPath:newPath error:&error] )
  468. {
  469. if( [[NSFileManager defaultManager] copyItemAtPath:resultPath toPath:newPath error:&error] )
  470. resultPath = newPath;
  471. else
  472. {
  473. NSLog( @"Error copying video: %@", error );
  474. resultPath = nil;
  475. }
  476. }
  477. else
  478. {
  479. NSLog( @"Error deleting existing video: %@", error );
  480. resultPath = nil;
  481. }
  482. }
  483. }
  484. }
  485. imagePicker = nil;
  486. imagePickerState = 2;
  487. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMediaReceived", [self getCString:resultPath] );
  488. [picker dismissViewControllerAnimated:NO completion:nil];
  489. }
  490. #pragma clang diagnostic pop
  491. // Credit: https://ikyle.me/blog/2020/phpickerviewcontroller
  492. +(void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14))
  493. {
  494. imagePickerNew = nil;
  495. imagePickerState = 2;
  496. [picker dismissViewControllerAnimated:NO completion:nil];
  497. if( results != nil && [results count] > 0 )
  498. {
  499. NSMutableArray<NSString *> *resultPaths = [NSMutableArray arrayWithCapacity:[results count]];
  500. NSLock *arrayLock = [[NSLock alloc] init];
  501. dispatch_group_t group = dispatch_group_create();
  502. for( int i = 0; i < [results count]; i++ )
  503. {
  504. PHPickerResult *result = results[i];
  505. NSItemProvider *itemProvider = result.itemProvider;
  506. NSString *assetIdentifier = result.assetIdentifier;
  507. __block NSString *resultPath = nil;
  508. int j = i + 1;
  509. //NSLog( @"result: %@", result );
  510. //NSLog( @"%@", result.assetIdentifier);
  511. //NSLog( @"%@", result.itemProvider);
  512. if( [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage] )
  513. {
  514. NSLog( @"Picked an image" );
  515. if( !simpleMediaPickMode && assetIdentifier != nil )
  516. {
  517. PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:assetIdentifier] options:nil] firstObject];
  518. resultPath = [self trySavePHAsset:asset atIndex:j];
  519. }
  520. if( resultPath != nil )
  521. {
  522. [arrayLock lock];
  523. [resultPaths addObject:resultPath];
  524. [arrayLock unlock];
  525. }
  526. else
  527. {
  528. dispatch_group_enter( group );
  529. [itemProvider loadFileRepresentationForTypeIdentifier:(NSString *)kUTTypeImage completionHandler:^( NSURL *url, NSError *error )
  530. {
  531. if( url != nil )
  532. {
  533. // Copy the image to a temporary location because the returned image will be deleted by the OS after this callback is completed
  534. resultPath = [url path];
  535. NSString *newPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j] stringByAppendingPathExtension:[resultPath pathExtension]];
  536. if( ![[NSFileManager defaultManager] fileExistsAtPath:newPath] || [[NSFileManager defaultManager] removeItemAtPath:newPath error:&error] )
  537. {
  538. if( [[NSFileManager defaultManager] copyItemAtPath:resultPath toPath:newPath error:&error])
  539. resultPath = newPath;
  540. else
  541. {
  542. NSLog( @"Error copying image: %@", error );
  543. resultPath = nil;
  544. }
  545. }
  546. else
  547. {
  548. NSLog( @"Error deleting existing image: %@", error );
  549. resultPath = nil;
  550. }
  551. }
  552. else
  553. NSLog( @"Error getting the picked image's path: %@", error );
  554. if( resultPath != nil )
  555. {
  556. [arrayLock lock];
  557. [resultPaths addObject:resultPath];
  558. [arrayLock unlock];
  559. }
  560. else
  561. {
  562. if( [itemProvider canLoadObjectOfClass:[UIImage class]] )
  563. {
  564. dispatch_group_enter( group );
  565. [itemProvider loadObjectOfClass:[UIImage class] completionHandler:^( __kindof id<NSItemProviderReading> object, NSError *error )
  566. {
  567. if( object != nil && [object isKindOfClass:[UIImage class]] )
  568. {
  569. resultPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j] stringByAppendingPathExtension:@"png"];
  570. if( ![self saveImageAsPNG:(UIImage *)object toPath:resultPath] )
  571. {
  572. NSLog( @"Error creating PNG image" );
  573. resultPath = nil;
  574. }
  575. }
  576. else
  577. NSLog( @"Error generating UIImage from picked image: %@", error );
  578. [arrayLock lock];
  579. [resultPaths addObject:( resultPath != nil ? resultPath : @"" )];
  580. [arrayLock unlock];
  581. dispatch_group_leave( group );
  582. }];
  583. }
  584. else
  585. {
  586. NSLog( @"Can't generate UIImage from picked image" );
  587. [arrayLock lock];
  588. [resultPaths addObject:@""];
  589. [arrayLock unlock];
  590. }
  591. }
  592. dispatch_group_leave( group );
  593. }];
  594. }
  595. }
  596. else if( [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeLivePhoto] )
  597. {
  598. NSLog( @"Picked a live photo" );
  599. if( [itemProvider canLoadObjectOfClass:[UIImage class]] )
  600. {
  601. dispatch_group_enter( group );
  602. [itemProvider loadObjectOfClass:[UIImage class] completionHandler:^( __kindof id<NSItemProviderReading> object, NSError *error )
  603. {
  604. if( object != nil && [object isKindOfClass:[UIImage class]] )
  605. {
  606. resultPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j] stringByAppendingPathExtension:@"png"];
  607. if( ![self saveImageAsPNG:(UIImage *)object toPath:resultPath] )
  608. {
  609. NSLog( @"Error creating PNG image" );
  610. resultPath = nil;
  611. }
  612. }
  613. else
  614. NSLog( @"Error generating UIImage from picked live photo: %@", error );
  615. [arrayLock lock];
  616. [resultPaths addObject:( resultPath != nil ? resultPath : @"" )];
  617. [arrayLock unlock];
  618. dispatch_group_leave( group );
  619. }];
  620. }
  621. else if( [itemProvider canLoadObjectOfClass:[PHLivePhoto class]] )
  622. {
  623. dispatch_group_enter( group );
  624. [itemProvider loadObjectOfClass:[PHLivePhoto class] completionHandler:^( __kindof id<NSItemProviderReading> object, NSError *error )
  625. {
  626. if( object != nil && [object isKindOfClass:[PHLivePhoto class]] )
  627. {
  628. // Extract image data from live photo
  629. // Credit: https://stackoverflow.com/a/41341675/2373034
  630. NSArray<PHAssetResource*>* livePhotoResources = [PHAssetResource assetResourcesForLivePhoto:(PHLivePhoto *)object];
  631. PHAssetResource *livePhotoImage = nil;
  632. for( int k = 0; k < [livePhotoResources count]; k++ )
  633. {
  634. if( livePhotoResources[k].type == PHAssetResourceTypePhoto )
  635. {
  636. livePhotoImage = livePhotoResources[k];
  637. break;
  638. }
  639. }
  640. if( livePhotoImage == nil )
  641. {
  642. NSLog( @"Error extracting image data from live photo" );
  643. [arrayLock lock];
  644. [resultPaths addObject:@""];
  645. [arrayLock unlock];
  646. }
  647. else
  648. {
  649. dispatch_group_enter( group );
  650. NSString *originalFilename = livePhotoImage.originalFilename;
  651. if( originalFilename == nil || [originalFilename length] == 0 )
  652. resultPath = [NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j];
  653. else
  654. resultPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j] stringByAppendingPathExtension:[originalFilename pathExtension]];
  655. [[PHAssetResourceManager defaultManager] writeDataForAssetResource:livePhotoImage toFile:[NSURL fileURLWithPath:resultPath] options:nil completionHandler:^( NSError * _Nullable error2 )
  656. {
  657. if( error2 != nil )
  658. {
  659. NSLog( @"Error saving image data from live photo: %@", error2 );
  660. resultPath = nil;
  661. }
  662. [arrayLock lock];
  663. [resultPaths addObject:( resultPath != nil ? resultPath : @"" )];
  664. [arrayLock unlock];
  665. dispatch_group_leave( group );
  666. }];
  667. }
  668. }
  669. else
  670. {
  671. NSLog( @"Error generating PHLivePhoto from picked live photo: %@", error );
  672. [arrayLock lock];
  673. [resultPaths addObject:@""];
  674. [arrayLock unlock];
  675. }
  676. dispatch_group_leave( group );
  677. }];
  678. }
  679. else
  680. {
  681. NSLog( @"Can't convert picked live photo to still image" );
  682. [arrayLock lock];
  683. [resultPaths addObject:@""];
  684. [arrayLock unlock];
  685. }
  686. }
  687. else if( [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie] || [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeVideo] )
  688. {
  689. NSLog( @"Picked a video" );
  690. // Get the video file's path
  691. dispatch_group_enter( group );
  692. [itemProvider loadFileRepresentationForTypeIdentifier:([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie] ? (NSString *)kUTTypeMovie : (NSString *)kUTTypeVideo) completionHandler:^( NSURL *url, NSError *error )
  693. {
  694. if( url != nil )
  695. {
  696. // Copy the video to a temporary location because the returned video will be deleted by the OS after this callback is completed
  697. resultPath = [url path];
  698. NSString *newPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, j] stringByAppendingPathExtension:[resultPath pathExtension]];
  699. if( ![[NSFileManager defaultManager] fileExistsAtPath:newPath] || [[NSFileManager defaultManager] removeItemAtPath:newPath error:&error] )
  700. {
  701. if( [[NSFileManager defaultManager] copyItemAtPath:resultPath toPath:newPath error:&error])
  702. resultPath = newPath;
  703. else
  704. {
  705. NSLog( @"Error copying video: %@", error );
  706. resultPath = nil;
  707. }
  708. }
  709. else
  710. {
  711. NSLog( @"Error deleting existing video: %@", error );
  712. resultPath = nil;
  713. }
  714. }
  715. else
  716. NSLog( @"Error getting the picked video's path: %@", error );
  717. [arrayLock lock];
  718. [resultPaths addObject:( resultPath != nil ? resultPath : @"" )];
  719. [arrayLock unlock];
  720. dispatch_group_leave( group );
  721. }];
  722. }
  723. else
  724. {
  725. // Unknown media type picked?
  726. NSLog( @"Couldn't determine type of picked media: %@", itemProvider );
  727. [arrayLock lock];
  728. [resultPaths addObject:@""];
  729. [arrayLock unlock];
  730. }
  731. }
  732. dispatch_group_notify( group, dispatch_get_main_queue(),
  733. ^{
  734. if( !pickingMultipleFiles )
  735. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMediaReceived", [self getCString:resultPaths[0]] );
  736. else
  737. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMultipleMediaReceived", [self getCString:[resultPaths componentsJoinedByString:@">"]] );
  738. });
  739. }
  740. else
  741. {
  742. NSLog( @"No media picked" );
  743. if( !pickingMultipleFiles )
  744. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMediaReceived", "" );
  745. else
  746. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMultipleMediaReceived", "" );
  747. }
  748. }
  749. + (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  750. {
  751. NSLog( @"UIImagePickerController cancelled" );
  752. imagePicker = nil;
  753. UnitySendMessage( "NGMediaReceiveCallbackiOS", "OnMediaReceived", "" );
  754. [picker dismissViewControllerAnimated:NO completion:nil];
  755. }
  756. + (NSString *)trySavePHAsset:(PHAsset *)asset atIndex:(int)filenameIndex
  757. {
  758. if( asset == nil )
  759. return nil;
  760. __block NSString *resultPath = nil;
  761. PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
  762. options.synchronous = YES;
  763. options.version = PHImageRequestOptionsVersionCurrent;
  764. if( @available(iOS 13.0, *) )
  765. {
  766. [[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:asset options:options resultHandler:^( NSData *imageData, NSString *dataUTI, CGImagePropertyOrientation orientation, NSDictionary *imageInfo )
  767. {
  768. if( imageData != nil )
  769. resultPath = [self trySaveSourceImage:imageData withInfo:imageInfo atIndex:filenameIndex];
  770. else
  771. NSLog( @"Couldn't fetch raw image data" );
  772. }];
  773. }
  774. else
  775. {
  776. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^( NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *imageInfo )
  777. {
  778. if( imageData != nil )
  779. resultPath = [self trySaveSourceImage:imageData withInfo:imageInfo atIndex:filenameIndex];
  780. else
  781. NSLog( @"Couldn't fetch raw image data" );
  782. }];
  783. }
  784. return resultPath;
  785. }
  786. + (NSString *)trySaveSourceImage:(NSData *)imageData withInfo:(NSDictionary *)info atIndex:(int)filenameIndex
  787. {
  788. NSString *filePath = info[@"PHImageFileURLKey"];
  789. if( filePath != nil ) // filePath can actually be an NSURL, convert it to NSString
  790. filePath = [NSString stringWithFormat:@"%@", filePath];
  791. if( filePath == nil || [filePath length] == 0 )
  792. {
  793. filePath = info[@"PHImageFileUTIKey"];
  794. if( filePath != nil )
  795. filePath = [NSString stringWithFormat:@"%@", filePath];
  796. }
  797. NSString *resultPath;
  798. if( filePath == nil || [filePath length] == 0 )
  799. resultPath = [NSString stringWithFormat:@"%@%d", pickedMediaSavePath, filenameIndex];
  800. else
  801. resultPath = [[NSString stringWithFormat:@"%@%d", pickedMediaSavePath, filenameIndex] stringByAppendingPathExtension:[filePath pathExtension]];
  802. NSError *error;
  803. if( ![[NSFileManager defaultManager] fileExistsAtPath:resultPath] || [[NSFileManager defaultManager] removeItemAtPath:resultPath error:&error] )
  804. {
  805. if( ![imageData writeToFile:resultPath atomically:YES] )
  806. {
  807. NSLog( @"Error copying source image to file" );
  808. resultPath = nil;
  809. }
  810. }
  811. else
  812. {
  813. NSLog( @"Error deleting existing image: %@", error );
  814. resultPath = nil;
  815. }
  816. return resultPath;
  817. }
  818. // Credit: https://lists.apple.com/archives/cocoa-dev/2012/Jan/msg00052.html
  819. + (int)getMediaTypeFromExtension:(NSString *)extension
  820. {
  821. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag( kUTTagClassFilenameExtension, (__bridge CFStringRef) extension, NULL );
  822. // mediaType is a bitmask:
  823. // 1: image
  824. // 2: video
  825. // 4: audio (not supported)
  826. int result = 0;
  827. if( UTTypeConformsTo( fileUTI, kUTTypeImage ) || UTTypeConformsTo( fileUTI, kUTTypeLivePhoto ) )
  828. result = 1;
  829. else if( UTTypeConformsTo( fileUTI, kUTTypeMovie ) || UTTypeConformsTo( fileUTI, kUTTypeVideo ) )
  830. result = 2;
  831. else if( UTTypeConformsTo( fileUTI, kUTTypeAudio ) )
  832. result = 4;
  833. CFRelease( fileUTI );
  834. return result;
  835. }
  836. // Credit: https://stackoverflow.com/a/4170099/2373034
  837. + (NSArray *)getImageMetadata:(NSString *)path
  838. {
  839. int width = 0;
  840. int height = 0;
  841. int orientation = -1;
  842. CGImageSourceRef imageSource = CGImageSourceCreateWithURL( (__bridge CFURLRef) [NSURL fileURLWithPath:path], nil );
  843. if( imageSource != nil )
  844. {
  845. NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(__bridge NSString *)kCGImageSourceShouldCache];
  846. CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex( imageSource, 0, (__bridge CFDictionaryRef) options );
  847. CFRelease( imageSource );
  848. CGFloat widthF = 0.0f, heightF = 0.0f;
  849. if( imageProperties != nil )
  850. {
  851. if( CFDictionaryContainsKey( imageProperties, kCGImagePropertyPixelWidth ) )
  852. CFNumberGetValue( (CFNumberRef) CFDictionaryGetValue( imageProperties, kCGImagePropertyPixelWidth ), kCFNumberCGFloatType, &widthF );
  853. if( CFDictionaryContainsKey( imageProperties, kCGImagePropertyPixelHeight ) )
  854. CFNumberGetValue( (CFNumberRef) CFDictionaryGetValue( imageProperties, kCGImagePropertyPixelHeight ), kCFNumberCGFloatType, &heightF );
  855. if( CFDictionaryContainsKey( imageProperties, kCGImagePropertyOrientation ) )
  856. {
  857. CFNumberGetValue( (CFNumberRef) CFDictionaryGetValue( imageProperties, kCGImagePropertyOrientation ), kCFNumberIntType, &orientation );
  858. if( orientation > 4 )
  859. {
  860. // Landscape image
  861. CGFloat temp = widthF;
  862. widthF = heightF;
  863. heightF = temp;
  864. }
  865. }
  866. CFRelease( imageProperties );
  867. }
  868. width = (int) roundf( widthF );
  869. height = (int) roundf( heightF );
  870. }
  871. return [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:width], [NSNumber numberWithInt:height], [NSNumber numberWithInt:orientation], nil];
  872. }
  873. + (char *)getImageProperties:(NSString *)path
  874. {
  875. NSArray *metadata = [self getImageMetadata:path];
  876. int orientationUnity;
  877. int orientation = [metadata[2] intValue];
  878. // To understand the magic numbers, see ImageOrientation enum in NativeGallery.cs
  879. // and http://sylvana.net/jpegcrop/exif_orientation.html
  880. if( orientation == 1 )
  881. orientationUnity = 0;
  882. else if( orientation == 2 )
  883. orientationUnity = 4;
  884. else if( orientation == 3 )
  885. orientationUnity = 2;
  886. else if( orientation == 4 )
  887. orientationUnity = 6;
  888. else if( orientation == 5 )
  889. orientationUnity = 5;
  890. else if( orientation == 6 )
  891. orientationUnity = 1;
  892. else if( orientation == 7 )
  893. orientationUnity = 7;
  894. else if( orientation == 8 )
  895. orientationUnity = 3;
  896. else
  897. orientationUnity = -1;
  898. return [self getCString:[NSString stringWithFormat:@"%d>%d> >%d", [metadata[0] intValue], [metadata[1] intValue], orientationUnity]];
  899. }
  900. + (char *)getVideoProperties:(NSString *)path
  901. {
  902. CGSize size = CGSizeZero;
  903. float rotation = 0;
  904. long long duration = 0;
  905. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path] options:nil];
  906. if( asset != nil )
  907. {
  908. duration = (long long) round( CMTimeGetSeconds( [asset duration] ) * 1000 );
  909. CGAffineTransform transform = [asset preferredTransform];
  910. NSArray<AVAssetTrack *>* videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
  911. if( videoTracks != nil && [videoTracks count] > 0 )
  912. {
  913. size = [[videoTracks objectAtIndex:0] naturalSize];
  914. transform = [[videoTracks objectAtIndex:0] preferredTransform];
  915. }
  916. rotation = atan2( transform.b, transform.a ) * ( 180.0 / M_PI );
  917. }
  918. return [self getCString:[NSString stringWithFormat:@"%d>%d>%lld>%f", (int) roundf( size.width ), (int) roundf( size.height ), duration, rotation]];
  919. }
  920. + (char *)getVideoThumbnail:(NSString *)path savePath:(NSString *)savePath maximumSize:(int)maximumSize captureTime:(double)captureTime
  921. {
  922. AVAssetImageGenerator *thumbnailGenerator = [[AVAssetImageGenerator alloc] initWithAsset:[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil]];
  923. thumbnailGenerator.appliesPreferredTrackTransform = YES;
  924. thumbnailGenerator.maximumSize = CGSizeMake( (CGFloat) maximumSize, (CGFloat) maximumSize );
  925. thumbnailGenerator.requestedTimeToleranceBefore = kCMTimeZero;
  926. thumbnailGenerator.requestedTimeToleranceAfter = kCMTimeZero;
  927. if( captureTime < 0.0 )
  928. captureTime = 0.0;
  929. else
  930. {
  931. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path] options:nil];
  932. if( asset != nil )
  933. {
  934. double videoDuration = CMTimeGetSeconds( [asset duration] );
  935. if( videoDuration > 0.0 && captureTime >= videoDuration - 0.1 )
  936. {
  937. if( captureTime > videoDuration )
  938. captureTime = videoDuration;
  939. thumbnailGenerator.requestedTimeToleranceBefore = CMTimeMakeWithSeconds( 1.0, 600 );
  940. }
  941. }
  942. }
  943. NSError *error = nil;
  944. CGImageRef image = [thumbnailGenerator copyCGImageAtTime:CMTimeMakeWithSeconds( captureTime, 600 ) actualTime:nil error:&error];
  945. if( image == nil )
  946. {
  947. if( error != nil )
  948. NSLog( @"Error generating video thumbnail: %@", error );
  949. else
  950. NSLog( @"Error generating video thumbnail..." );
  951. return [self getCString:@""];
  952. }
  953. UIImage *thumbnail = [[UIImage alloc] initWithCGImage:image];
  954. CGImageRelease( image );
  955. if( ![UIImagePNGRepresentation( thumbnail ) writeToFile:savePath atomically:YES] )
  956. {
  957. NSLog( @"Error saving thumbnail image" );
  958. return [self getCString:@""];
  959. }
  960. return [self getCString:savePath];
  961. }
  962. + (BOOL)saveImageAsPNG:(UIImage *)image toPath:(NSString *)resultPath
  963. {
  964. return [UIImagePNGRepresentation( [self scaleImage:image maxSize:16384] ) writeToFile:resultPath atomically:YES];
  965. }
  966. + (UIImage *)scaleImage:(UIImage *)image maxSize:(int)maxSize
  967. {
  968. CGFloat width = image.size.width;
  969. CGFloat height = image.size.height;
  970. UIImageOrientation orientation = image.imageOrientation;
  971. if( width <= maxSize && height <= maxSize && orientation != UIImageOrientationDown &&
  972. orientation != UIImageOrientationLeft && orientation != UIImageOrientationRight &&
  973. orientation != UIImageOrientationLeftMirrored && orientation != UIImageOrientationRightMirrored &&
  974. orientation != UIImageOrientationUpMirrored && orientation != UIImageOrientationDownMirrored )
  975. return image;
  976. CGFloat scaleX = 1.0f;
  977. CGFloat scaleY = 1.0f;
  978. if( width > maxSize )
  979. scaleX = maxSize / width;
  980. if( height > maxSize )
  981. scaleY = maxSize / height;
  982. // Credit: https://github.com/mbcharbonneau/UIImage-Categories/blob/master/UIImage%2BAlpha.m
  983. CGImageAlphaInfo alpha = CGImageGetAlphaInfo( image.CGImage );
  984. BOOL hasAlpha = alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast || alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast;
  985. CGFloat scaleRatio = scaleX < scaleY ? scaleX : scaleY;
  986. CGRect imageRect = CGRectMake( 0, 0, width * scaleRatio, height * scaleRatio );
  987. UIGraphicsImageRendererFormat *format = [image imageRendererFormat];
  988. format.opaque = !hasAlpha;
  989. format.scale = image.scale;
  990. UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:imageRect.size format:format];
  991. image = [renderer imageWithActions:^( UIGraphicsImageRendererContext* _Nonnull myContext )
  992. {
  993. [image drawInRect:imageRect];
  994. }];
  995. return image;
  996. }
  997. + (char *)loadImageAtPath:(NSString *)path tempFilePath:(NSString *)tempFilePath maximumSize:(int)maximumSize
  998. {
  999. // Check if the image can be loaded by Unity without requiring a conversion to PNG
  1000. // Credit: https://stackoverflow.com/a/12048937/2373034
  1001. NSString *extension = [path pathExtension];
  1002. BOOL conversionNeeded = [extension caseInsensitiveCompare:@"jpg"] != NSOrderedSame && [extension caseInsensitiveCompare:@"jpeg"] != NSOrderedSame && [extension caseInsensitiveCompare:@"png"] != NSOrderedSame;
  1003. if( !conversionNeeded )
  1004. {
  1005. // Check if the image needs to be processed at all
  1006. NSArray *metadata = [self getImageMetadata:path];
  1007. int orientationInt = [metadata[2] intValue]; // 1: correct orientation, [1,8]: valid orientation range
  1008. if( orientationInt == 1 && [metadata[0] intValue] <= maximumSize && [metadata[1] intValue] <= maximumSize )
  1009. return [self getCString:path];
  1010. }
  1011. UIImage *image = [UIImage imageWithContentsOfFile:path];
  1012. if( image == nil )
  1013. return [self getCString:path];
  1014. UIImage *scaledImage = [self scaleImage:image maxSize:maximumSize];
  1015. if( conversionNeeded || scaledImage != image )
  1016. {
  1017. if( ![UIImagePNGRepresentation( scaledImage ) writeToFile:tempFilePath atomically:YES] )
  1018. {
  1019. NSLog( @"Error creating scaled image" );
  1020. return [self getCString:path];
  1021. }
  1022. return [self getCString:tempFilePath];
  1023. }
  1024. else
  1025. return [self getCString:path];
  1026. }
  1027. // Credit: https://stackoverflow.com/a/37052118/2373034
  1028. + (char *)getCString:(NSString *)source
  1029. {
  1030. if( source == nil )
  1031. source = @"";
  1032. const char *sourceUTF8 = [source UTF8String];
  1033. char *result = (char*) malloc( strlen( sourceUTF8 ) + 1 );
  1034. strcpy( result, sourceUTF8 );
  1035. return result;
  1036. }
  1037. @end
  1038. extern "C" int _NativeGallery_CheckPermission( int readPermission, int permissionFreeMode )
  1039. {
  1040. return [UNativeGallery checkPermission:( readPermission == 1 ) permissionFreeMode:( permissionFreeMode == 1 )];
  1041. }
  1042. extern "C" void _NativeGallery_RequestPermission( int readPermission, int permissionFreeMode )
  1043. {
  1044. [UNativeGallery requestPermission:( readPermission == 1 ) permissionFreeMode:( permissionFreeMode == 1 )];
  1045. }
  1046. extern "C" void _NativeGallery_ShowLimitedLibraryPicker()
  1047. {
  1048. return [UNativeGallery showLimitedLibraryPicker];
  1049. }
  1050. extern "C" void _NativeGallery_OpenSettings()
  1051. {
  1052. [UNativeGallery openSettings];
  1053. }
  1054. extern "C" int _NativeGallery_CanPickMultipleMedia()
  1055. {
  1056. return [UNativeGallery canPickMultipleMedia];
  1057. }
  1058. extern "C" void _NativeGallery_ImageWriteToAlbum( const char* path, const char* album, int permissionFreeMode )
  1059. {
  1060. [UNativeGallery saveMedia:[NSString stringWithUTF8String:path] albumName:[NSString stringWithUTF8String:album] isImage:YES permissionFreeMode:( permissionFreeMode == 1 )];
  1061. }
  1062. extern "C" void _NativeGallery_VideoWriteToAlbum( const char* path, const char* album, int permissionFreeMode )
  1063. {
  1064. [UNativeGallery saveMedia:[NSString stringWithUTF8String:path] albumName:[NSString stringWithUTF8String:album] isImage:NO permissionFreeMode:( permissionFreeMode == 1 )];
  1065. }
  1066. extern "C" void _NativeGallery_PickMedia( const char* mediaSavePath, int mediaType, int permissionFreeMode, int selectionLimit )
  1067. {
  1068. [UNativeGallery pickMedia:mediaType savePath:[NSString stringWithUTF8String:mediaSavePath] permissionFreeMode:( permissionFreeMode == 1 ) selectionLimit:selectionLimit];
  1069. }
  1070. extern "C" int _NativeGallery_IsMediaPickerBusy()
  1071. {
  1072. return [UNativeGallery isMediaPickerBusy];
  1073. }
  1074. extern "C" int _NativeGallery_GetMediaTypeFromExtension( const char* extension )
  1075. {
  1076. return [UNativeGallery getMediaTypeFromExtension:[NSString stringWithUTF8String:extension]];
  1077. }
  1078. extern "C" char* _NativeGallery_GetImageProperties( const char* path )
  1079. {
  1080. return [UNativeGallery getImageProperties:[NSString stringWithUTF8String:path]];
  1081. }
  1082. extern "C" char* _NativeGallery_GetVideoProperties( const char* path )
  1083. {
  1084. return [UNativeGallery getVideoProperties:[NSString stringWithUTF8String:path]];
  1085. }
  1086. extern "C" char* _NativeGallery_GetVideoThumbnail( const char* path, const char* thumbnailSavePath, int maxSize, double captureTimeInSeconds )
  1087. {
  1088. return [UNativeGallery getVideoThumbnail:[NSString stringWithUTF8String:path] savePath:[NSString stringWithUTF8String:thumbnailSavePath] maximumSize:maxSize captureTime:captureTimeInSeconds];
  1089. }
  1090. extern "C" char* _NativeGallery_LoadImageAtPath( const char* path, const char* temporaryFilePath, int maxSize )
  1091. {
  1092. return [UNativeGallery loadImageAtPath:[NSString stringWithUTF8String:path] tempFilePath:[NSString stringWithUTF8String:temporaryFilePath] maximumSize:maxSize];
  1093. }