FLEXImageShortcuts.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // FLEXImageShortcuts.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/29/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXImageShortcuts.h"
  9. #import "FLEXImagePreviewViewController.h"
  10. #import "FLEXShortcut.h"
  11. #import "FLEXAlert.h"
  12. @interface UIAlertController (FLEXImageShortcuts)
  13. - (void)flex_image:(UIImage *)image disSaveWithError:(NSError *)error :(void *)context;
  14. @end
  15. @implementation FLEXImageShortcuts
  16. #pragma mark - Overrides
  17. + (instancetype)forObject:(UIImage *)image {
  18. // These additional rows will appear at the beginning of the shortcuts section.
  19. // The methods below are written in such a way that they will not interfere
  20. // with properties/etc being registered alongside these
  21. return [self forObject:image additionalRows:@[
  22. [FLEXActionShortcut title:@"View Image" subtitle:nil
  23. viewer:^UIViewController *(id image) {
  24. return [FLEXImagePreviewViewController forImage:image];
  25. }
  26. accessoryType:^UITableViewCellAccessoryType(id image) {
  27. return UITableViewCellAccessoryDisclosureIndicator;
  28. }
  29. ],
  30. [FLEXActionShortcut title:@"Save Image" subtitle:nil
  31. selectionHandler:^(UIViewController *host, id image) {
  32. // Present modal alerting user about saving
  33. UIAlertController *alert = [FLEXAlert makeAlert:^(FLEXAlert *make) {
  34. make.title(@"Saving Image…");
  35. }];
  36. [host presentViewController:alert animated:YES completion:nil];
  37. #if !TARGET_OS_TV
  38. // Save the image
  39. UIImageWriteToSavedPhotosAlbum(
  40. image, alert, @selector(flex_image:disSaveWithError::), nil
  41. );
  42. #else
  43. //FIXME: do something else for tvOS
  44. NSLog(@"not saving image to photo album, tvOS!");
  45. #endif
  46. }
  47. accessoryType:^UITableViewCellAccessoryType(id image) {
  48. return UITableViewCellAccessoryDisclosureIndicator;
  49. }
  50. ]
  51. ]];
  52. }
  53. @end
  54. @implementation UIAlertController (FLEXImageShortcuts)
  55. - (void)flex_image:(UIImage *)image disSaveWithError:(NSError *)error :(void *)context {
  56. self.title = @"Image Saved";
  57. flex_dispatch_after(1, dispatch_get_main_queue(), ^{
  58. [self dismissViewControllerAnimated:YES completion:nil];
  59. });
  60. }
  61. @end