FLEXImagePreviewViewController.m 3.4 KB

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