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.

121 lines
3.9 KiB

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