FLEXImagePreviewViewController.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "FLEXImagePreviewViewController.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXImagePreviewViewController () <UIScrollViewDelegate>
  11. @property (nonatomic, strong) UIImage *image;
  12. @property (nonatomic, strong) UIScrollView *scrollView;
  13. @property (nonatomic, strong) UIImageView *imageView;
  14. @end
  15. @implementation FLEXImagePreviewViewController
  16. - (id)initWithImage:(UIImage *)image
  17. {
  18. self = [super initWithNibName:nil bundle:nil];
  19. if (self) {
  20. self.title = @"Preview";
  21. self.image = image;
  22. }
  23. return self;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = [FLEXUtility scrollViewGrayColor];
  29. self.imageView = [[UIImageView alloc] initWithImage:self.image];
  30. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  31. self.scrollView.delegate = self;
  32. self.scrollView.backgroundColor = self.view.backgroundColor;
  33. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  34. [self.scrollView addSubview:self.imageView];
  35. self.scrollView.contentSize = self.imageView.frame.size;
  36. self.scrollView.minimumZoomScale = 1.0;
  37. self.scrollView.maximumZoomScale = 2.0;
  38. [self.view addSubview:self.scrollView];
  39. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];
  40. }
  41. - (void)viewDidLayoutSubviews
  42. {
  43. [self centerContentInScrollViewIfNeeded];
  44. }
  45. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  46. {
  47. return self.imageView;
  48. }
  49. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  50. {
  51. [self centerContentInScrollViewIfNeeded];
  52. }
  53. - (void)centerContentInScrollViewIfNeeded
  54. {
  55. CGFloat horizontalInset = 0.0;
  56. CGFloat verticalInset = 0.0;
  57. if (self.scrollView.contentSize.width < self.scrollView.bounds.size.width) {
  58. horizontalInset = (self.scrollView.bounds.size.width - self.scrollView.contentSize.width) / 2.0;
  59. }
  60. if (self.scrollView.contentSize.height < self.scrollView.bounds.size.height) {
  61. verticalInset = (self.scrollView.bounds.size.height - self.scrollView.contentSize.height) / 2.0;
  62. }
  63. self.scrollView.contentInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
  64. }
  65. - (void)actionButtonPressed:(id)sender
  66. {
  67. static BOOL CanSaveToCameraRoll = NO;
  68. static dispatch_once_t onceToken;
  69. dispatch_once(&onceToken, ^{
  70. if ([UIDevice currentDevice].systemVersion.floatValue < 10) {
  71. CanSaveToCameraRoll = YES;
  72. return;
  73. }
  74. NSBundle *mainBundle = [NSBundle mainBundle];
  75. if ([mainBundle.infoDictionary.allKeys containsObject:@"NSPhotoLibraryUsageDescription"]) {
  76. CanSaveToCameraRoll = YES;
  77. } else {
  78. NSLog(@"Add NSPhotoLibraryUsageDescription in app's Info.plist for saving captured image into camera roll.");
  79. }
  80. });
  81. UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self.image] applicationActivities:@[]];
  82. if (!CanSaveToCameraRoll) {
  83. activityVC.excludedActivityTypes = @[UIActivityTypeSaveToCameraRoll];
  84. }
  85. [self presentViewController:activityVC animated:YES completion:nil];
  86. }
  87. @end