forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulImagePreviewViewController.m
More file actions
315 lines (272 loc) · 11.2 KB
/
AwfulImagePreviewViewController.m
File metadata and controls
315 lines (272 loc) · 11.2 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// AwfulImagePreviewViewController.m
// Awful
//
// Created by Nolan Waite on 2012-11-14.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulImagePreviewViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "AwfulActionSheet.h"
#import "AwfulAlertView.h"
#import "FVGifAnimation.h"
#import "SVProgressHUD.h"
#import "UIImageView+AFNetworking.h"
#import "UINavigationItem+TwoLineTitle.h"
@interface AwfulImagePreviewViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@property (nonatomic) NSOperationQueue *queue;
@property (nonatomic) NSData *imageData;
@property (nonatomic) UIStatusBarStyle statusBarStyle;
@property (nonatomic) NSTimer *automaticallyHideBarsTimer;
@property (nonatomic) UIBarButtonItem *doneButton;
@property (nonatomic) UIBarButtonItem *actionButton;
@end
@implementation AwfulImagePreviewViewController
- (id)initWithURL:(NSURL *)imageURL
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
_imageURL = imageURL;
self.wantsFullScreenLayout = YES;
self.navigationItem.leftBarButtonItem = self.doneButton;
self.navigationItem.rightBarButtonItem = self.actionButton;
_queue = [NSOperationQueue new];
_queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
}
return self;
}
- (void)setImageURL:(NSURL *)imageURL
{
if ([_imageURL isEqual:imageURL]) return;
_imageURL = imageURL;
[self updateImageView];
}
- (UIBarButtonItem *)doneButton
{
if (_doneButton) return _doneButton;
_doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(done)];
return _doneButton;
}
- (UIBarButtonItem *)actionButton
{
if (_actionButton) return _actionButton;
_actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(showActions)];
return _actionButton;
}
- (void)updateImageView
{
if (!self.imageURL) return;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.imageURL];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseData) {
self.imageData = responseData;
FVGifAnimation *animation = [[FVGifAnimation alloc] initWithData:responseData];
if ([animation canAnimate]) {
[animation setAnimationToImageView:self.imageView];
[self.imageView startAnimating];
} else {
self.imageView.image = [UIImage imageWithData:responseData];
}
self.imageView.backgroundColor = [UIColor whiteColor];
[self.imageView sizeToFit];
self.scrollView.contentSize = self.imageView.bounds.size;
self.scrollView.minimumZoomScale = [self minimumZoomScale];
self.scrollView.zoomScale = [self minimumZoomScale];
self.scrollView.maximumZoomScale = 40;
[self centerImageInScrollView];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[AwfulAlertView showWithTitle:@"Could Not Load Image" error:error buttonTitle:@"OK"];
}];
[self.queue addOperation:op];
}
- (CGFloat)minimumZoomScale
{
CGFloat horizontal = self.scrollView.bounds.size.width / self.imageView.image.size.width;
CGFloat vertical = self.scrollView.bounds.size.height / self.imageView.image.size.height;
return MIN(MIN(horizontal, vertical), 1);
}
- (void)toggleVisibleBars
{
[self.automaticallyHideBarsTimer invalidate];
if (self.navigationController.navigationBarHidden) {
self.scrollView.contentInset = UIEdgeInsetsZero;
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationNone];
[self.navigationController setNavigationBarHidden:NO animated:NO];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];
CGFloat oldAlpha = self.navigationController.navigationBar.alpha;
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
self.navigationController.navigationBar.alpha = 0;
} completion:^(BOOL finished) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.navigationBar.alpha = oldAlpha;
}];
}
}
- (void)done
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)showActions
{
[self.automaticallyHideBarsTimer invalidate];
AwfulActionSheet *sheet = [AwfulActionSheet new];
[sheet addButtonWithTitle:@"Save to Photos" block:^{
[SVProgressHUD showWithStatus:@"Saving…" maskType:SVProgressHUDMaskTypeGradient];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ALAssetsLibrary *assets = [ALAssetsLibrary new];
[assets writeImageDataToSavedPhotosAlbum:self.imageData
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[SVProgressHUD dismiss];
[AwfulAlertView showWithTitle:@"Could Not Save Image"
error:error
buttonTitle:@"OK"
completion:^{ [self hideBarsAfterShortDuration]; }];
} else {
[SVProgressHUD showSuccessWithStatus:@"Saved"];
}
});
}];
});
}];
[sheet addButtonWithTitle:@"Copy Image URL" block:^{
[UIPasteboard generalPasteboard].items = @[ @{
(id)kUTTypeURL: self.imageURL,
(id)kUTTypePlainText: [self.imageURL absoluteString]
}];
[self hideBarsAfterShortDuration];
}];
[sheet addCancelButtonWithTitle:@"Cancel" block:^{
[self hideBarsAfterShortDuration];
}];
[sheet showFromBarButtonItem:self.actionButton animated:YES];
}
- (void)hideBarsAfterShortDuration
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:6
target:self
selector:@selector(toggleVisibleBars)
userInfo:nil
repeats:NO];
self.automaticallyHideBarsTimer = timer;
}
- (void)centerImageInScrollView
{
CGPoint center = CGPointMake(self.scrollView.contentSize.width / 2,
self.scrollView.contentSize.height / 2);
if (self.scrollView.bounds.size.width > self.scrollView.contentSize.width) {
center.x += (self.scrollView.bounds.size.width - self.scrollView.contentSize.width) / 2;
}
if (self.scrollView.bounds.size.height > self.scrollView.contentSize.height) {
center.y += (self.scrollView.bounds.size.height - self.scrollView.contentSize.height) / 2;
}
self.imageView.center = center;
}
#pragma mark - UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self initWithURL:nil];
}
- (void)setTitle:(NSString *)title
{
[super setTitle:title];
self.navigationItem.titleLabel.text = title;
}
- (void)loadView
{
UIScrollView *scrollView = [UIScrollView new];
self.scrollView = scrollView;
scrollView.frame = [UIScreen mainScreen].bounds;
scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [UIImageView new];
imageView.frame = (CGRect){ .size = scrollView.frame.size };
[scrollView addSubview:imageView];
self.imageView = imageView;
UIView *parent = [UIView new];
parent.frame = scrollView.frame;
parent.autoresizingMask = scrollView.autoresizingMask;
[parent addSubview:scrollView];
self.view = parent;
UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
[tap addTarget:self action:@selector(toggleVisibleBars)];
[self.view addGestureRecognizer:tap];
UISwipeGestureRecognizer *swipe = [UISwipeGestureRecognizer new];
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[swipe addTarget:self action:@selector(done)];
[self.view addGestureRecognizer:swipe];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateImageView];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
self.statusBarStyle = [UIApplication sharedApplication].statusBarStyle;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent
animated:YES];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self hideBarsAfterShortDuration];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.automaticallyHideBarsTimer invalidate];
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
[[UIApplication sharedApplication] setStatusBarStyle:self.statusBarStyle animated:YES];
}
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationNone];
[super viewWillDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return YES;
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
self.scrollView.minimumZoomScale = [self minimumZoomScale];
if (self.scrollView.zoomScale < self.scrollView.minimumZoomScale) {
self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
}
[self centerImageInScrollView];
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
[self centerImageInScrollView];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view
atScale:(float)scale
{
// Implemented so zooming works.
}
@end