FLEXImagePreviewViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // FLEXImagePreviewViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/12/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXImagePreviewViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXColor.h"
  11. #import "FLEXResources.h"
  12. @interface FLEXImagePreviewViewController () <UIScrollViewDelegate>
  13. @property (nonatomic) UIImage *image;
  14. @property (nonatomic) UIScrollView *scrollView;
  15. @property (nonatomic) UIImageView *imageView;
  16. @property (nonatomic) UITapGestureRecognizer *bgColorTapGesture;
  17. @property (nonatomic) NSInteger backgroundColorIndex;
  18. @property (nonatomic, readonly) NSArray<UIColor *> *backgroundColors;
  19. @end
  20. #pragma mark -
  21. @implementation FLEXImagePreviewViewController
  22. #pragma mark Initialization
  23. + (instancetype)previewForView:(UIView *)view {
  24. return [self forImage:[FLEXUtility previewImageForView:view]];
  25. }
  26. + (instancetype)previewForLayer:(CALayer *)layer {
  27. return [self forImage:[FLEXUtility previewImageForLayer:layer]];
  28. }
  29. + (instancetype)forImage:(UIImage *)image {
  30. return [[self alloc] initWithImage:image];
  31. }
  32. - (id)initWithImage:(UIImage *)image {
  33. NSParameterAssert(image);
  34. self = [super init];
  35. if (self) {
  36. self.title = @"Preview";
  37. self.image = image;
  38. _backgroundColors = @[FLEXResources.checkerPatternColor, UIColor.whiteColor, UIColor.blackColor];
  39. }
  40. return self;
  41. }
  42. #pragma mark Lifecycle
  43. - (void)viewDidLoad {
  44. [super viewDidLoad];
  45. self.imageView = [[UIImageView alloc] initWithImage:self.image];
  46. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  47. self.scrollView.delegate = self;
  48. self.scrollView.backgroundColor = self.backgroundColors.firstObject;
  49. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  50. [self.scrollView addSubview:self.imageView];
  51. self.scrollView.contentSize = self.imageView.frame.size;
  52. self.scrollView.minimumZoomScale = 1.0;
  53. self.scrollView.maximumZoomScale = 2.0;
  54. [self.view addSubview:self.scrollView];
  55. self.bgColorTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackground)];
  56. [self.scrollView addGestureRecognizer:self.bgColorTapGesture];
  57. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
  58. initWithBarButtonSystemItem:UIBarButtonSystemItemAction
  59. target:self
  60. action:@selector(actionButtonPressed:)
  61. ];
  62. }
  63. - (void)viewDidLayoutSubviews {
  64. [self centerContentInScrollViewIfNeeded];
  65. }
  66. #pragma mark UIScrollViewDelegate
  67. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  68. return self.imageView;
  69. }
  70. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  71. [self centerContentInScrollViewIfNeeded];
  72. }
  73. #pragma mark Private
  74. - (void)centerContentInScrollViewIfNeeded {
  75. CGFloat horizontalInset = 0.0;
  76. CGFloat verticalInset = 0.0;
  77. if (self.scrollView.contentSize.width < self.scrollView.bounds.size.width) {
  78. horizontalInset = (self.scrollView.bounds.size.width - self.scrollView.contentSize.width) / 2.0;
  79. }
  80. if (self.scrollView.contentSize.height < self.scrollView.bounds.size.height) {
  81. verticalInset = (self.scrollView.bounds.size.height - self.scrollView.contentSize.height) / 2.0;
  82. }
  83. self.scrollView.contentInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
  84. }
  85. - (void)changeBackground {
  86. self.backgroundColorIndex++;
  87. self.backgroundColorIndex %= self.backgroundColors.count;
  88. self.scrollView.backgroundColor = self.backgroundColors[self.backgroundColorIndex];
  89. }
  90. - (void)actionButtonPressed:(id)sender {
  91. static BOOL canSaveToCameraRoll = NO, didShowWarning = NO;
  92. static dispatch_once_t onceToken;
  93. dispatch_once(&onceToken, ^{
  94. if (UIDevice.currentDevice.systemVersion.floatValue < 10) {
  95. canSaveToCameraRoll = YES;
  96. return;
  97. }
  98. NSBundle *mainBundle = NSBundle.mainBundle;
  99. if ([mainBundle.infoDictionary.allKeys containsObject:@"NSPhotoLibraryUsageDescription"]) {
  100. canSaveToCameraRoll = YES;
  101. }
  102. });
  103. UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self.image] applicationActivities:@[]];
  104. if (!canSaveToCameraRoll && !didShowWarning) {
  105. didShowWarning = YES;
  106. NSString *msg = @"Add 'NSPhotoLibraryUsageDescription' to this app's Info.plist to save images.";
  107. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  108. make.title(@"Reminder").message(msg);
  109. make.button(@"OK").handler(^(NSArray<NSString *> *strings) {
  110. [self presentViewController:activityVC animated:YES completion:nil];
  111. });
  112. } showFrom:self];
  113. } else {
  114. [self presentViewController:activityVC animated:YES completion:nil];
  115. }
  116. }
  117. @end