-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFLEXImagePreviewViewController.m
More file actions
158 lines (126 loc) · 5.39 KB
/
FLEXImagePreviewViewController.m
File metadata and controls
158 lines (126 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// FLEXImagePreviewViewController.m
// Flipboard
//
// Created by Ryan Olson on 6/12/14.
// Copyright (c) 2020 FLEX Team. All rights reserved.
//
#import "FLEXImagePreviewViewController.h"
#import "FLEXActivityViewController.h"
#import "FLEXUtility.h"
#import "FLEXColor.h"
#import "FLEXResources.h"
@interface FLEXImagePreviewViewController () <UIScrollViewDelegate>
@property (nonatomic) UIImage *image;
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UIImageView *imageView;
@property (nonatomic) UITapGestureRecognizer *bgColorTapGesture;
@property (nonatomic) NSInteger backgroundColorIndex;
@property (nonatomic, readonly) NSArray<UIColor *> *backgroundColors;
@end
#pragma mark -
@implementation FLEXImagePreviewViewController
#pragma mark Initialization
+ (instancetype)previewForView:(UIView *)view {
return [self forImage:[FLEXUtility previewImageForView:view]];
}
+ (instancetype)previewForLayer:(CALayer *)layer {
return [self forImage:[FLEXUtility previewImageForLayer:layer]];
}
+ (instancetype)forImage:(UIImage *)image {
return [[self alloc] initWithImage:image];
}
- (id)initWithImage:(UIImage *)image {
NSParameterAssert(image);
self = [super init];
if (self) {
self.title = @"Preview";
self.image = image;
_backgroundColors = @[FLEXResources.checkerPatternColor, UIColor.whiteColor, UIColor.blackColor];
}
return self;
}
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
[super dismissViewControllerAnimated:flag completion:completion];
}
#pragma mark Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Force LTR layout for UI consistency
self.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.navigationController.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.imageView = [[UIImageView alloc] initWithImage:self.image];
self.imageView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.scrollView.delegate = self;
self.scrollView.backgroundColor = self.backgroundColors.firstObject;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = self.imageView.frame.size;
self.scrollView.minimumZoomScale = 1.0;
self.scrollView.maximumZoomScale = 2.0;
[self.view addSubview:self.scrollView];
self.bgColorTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackground)];
[self.scrollView addGestureRecognizer:self.bgColorTapGesture];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(actionButtonPressed:)
];
}
- (void)viewDidLayoutSubviews {
[self centerContentInScrollViewIfNeeded];
}
#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self centerContentInScrollViewIfNeeded];
}
#pragma mark Private
- (void)centerContentInScrollViewIfNeeded {
CGFloat horizontalInset = 0.0;
CGFloat verticalInset = 0.0;
if (self.scrollView.contentSize.width < self.scrollView.bounds.size.width) {
horizontalInset = (self.scrollView.bounds.size.width - self.scrollView.contentSize.width) / 2.0;
}
if (self.scrollView.contentSize.height < self.scrollView.bounds.size.height) {
verticalInset = (self.scrollView.bounds.size.height - self.scrollView.contentSize.height) / 2.0;
}
self.scrollView.contentInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
}
- (void)changeBackground {
self.backgroundColorIndex++;
self.backgroundColorIndex %= self.backgroundColors.count;
self.scrollView.backgroundColor = self.backgroundColors[self.backgroundColorIndex];
}
- (void)actionButtonPressed:(id)sender {
static BOOL canSaveToCameraRoll = NO, didShowWarning = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (UIDevice.currentDevice.systemVersion.floatValue < 10) {
canSaveToCameraRoll = YES;
return;
}
NSBundle *mainBundle = NSBundle.mainBundle;
if ([mainBundle.infoDictionary.allKeys containsObject:@"NSPhotoLibraryUsageDescription"]) {
canSaveToCameraRoll = YES;
}
});
UIViewController *activityVC = [FLEXActivityViewController sharing:@[self.image] source:sender];
if (!canSaveToCameraRoll && !didShowWarning) {
didShowWarning = YES;
NSString *msg = @"Add 'NSPhotoLibraryUsageDescription' to this app's Info.plist to save images.";
[FLEXAlert makeAlert:^(FLEXAlert *make) {
make.title(@"Reminder").message(msg);
make.button(@"OK").handler(^(NSArray<NSString *> *strings) {
[self presentViewController:activityVC animated:YES completion:nil];
});
} showFrom:self];
} else {
[self presentViewController:activityVC animated:YES completion:nil];
}
}
@end