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
//
// JTImagePreviewer.m
// JobTalk
//
// Created by Xummer on 14-7-1.
// Copyright (c) 2014年 BST. All rights reserved.
//
#define PRE_IMAGE_SVAE_BTN_WIDTH (44)
#import "JTImagePreviewer.h"
#import "UIImageView+WebCache.h"
#import "IBTLoadingView.h"
@interface JTImagePreviewer ()
<
UIScrollViewDelegate
>
{
NSURL *_url;
CGRect _startRect;
UITapGestureRecognizer *_tapGestureRecognizer;
}
@property (strong, nonatomic) IBTUIView *bgView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) IBTUIView *toolBarContainer;
@property (strong, nonatomic) UIButton *saveButton;
@property (weak, nonatomic) UIImage *tempImage;
@property (weak, nonatomic) UIViewController *viewController;
@end
@implementation JTImagePreviewer
#pragma mark - Life Cycle
- (id)initWithViewController:(UIViewController *)viewController
tempImage:(UIImage *)image
url:(NSURL *)url
startRect:(CGRect)rect
{
CGRect frame = CGRectZero;
UIViewController *vCtrl = viewController;
while (vCtrl.navigationController) {
vCtrl = vCtrl.navigationController;
}
if (vCtrl.tabBarController) {
vCtrl = vCtrl.tabBarController;
}
frame = vCtrl.view.bounds;
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
_url = url;
_startRect = rect;
self.viewController = viewController;
self.tempImage = image;
[self _init];
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _init];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_scrollView.frame = _bgView.bounds;
_scrollView.contentSize = _bgView.bounds.size;
_imageView.frame = _bgView.bounds;
_toolBarContainer.frame = (CGRect){
.origin.x = 0,
.origin.y = CGRectGetMaxY(_scrollView.frame) - PRE_IMAGE_SVAE_BTN_WIDTH,
.size.width = CGRectGetWidth(_bgView.bounds),
.size.height = PRE_IMAGE_SVAE_BTN_WIDTH
};
_saveButton.frame = (CGRect){
.origin.x = (CGRectGetWidth(_toolBarContainer.bounds) - PRE_IMAGE_SVAE_BTN_WIDTH) * .5f,
.origin.y = 0,
.size.width = PRE_IMAGE_SVAE_BTN_WIDTH,
.size.height = PRE_IMAGE_SVAE_BTN_WIDTH
};
}
#pragma mark - Private Method
- (void)_init {
self.bgView = [[IBTUIView alloc] init];
_bgView.frame = self.bounds;
_bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_bgView.backgroundColor = [UIColor colorWithW:0 a:1];
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[_bgView addGestureRecognizer:_tapGestureRecognizer];
self.scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.delegate = self;
_scrollView.maximumZoomScale = 2.6;
_scrollView.minimumZoomScale = 1;
_scrollView.zoomScale = 1;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
self.imageView = [[UIImageView alloc] init];
_imageView.backgroundColor = [UIColor clearColor];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
@autoreleasepool {
[_imageView sd_setImageWithURL:_url placeholderImage:_tempImage];
}
self.toolBarContainer = [[IBTUIView alloc] init];
_toolBarContainer.backgroundColor = [UIColor colorWithW:0 a:.5f];
self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_saveButton setImage:[UIImage imageNamed:@"save_to_album"]
forState:UIControlStateNormal];
_saveButton.showsTouchWhenHighlighted = YES;
[_saveButton addTarget:self
action:@selector(save)
forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:_imageView];
[_bgView addSubview:_scrollView];
[_toolBarContainer addSubview:_saveButton];
[_bgView addSubview:_toolBarContainer];
[self addSubview:_bgView];
}
- (void)dealloc {
[self cleanup];
}
#pragma mark - Action
- (void)cleanup {
_url = nil;
_tapGestureRecognizer = nil;
}
- (void)save {
if (_imageView.image) {
[IBTLoadingView showProgressLabel:@"保存中..."];
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
- (void)hide {
self.userInteractionEnabled = NO;
[UIView animateWithDuration:.2f
animations:^{
_imageView.frame = _startRect;
self.alpha = 0;
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)show {
self.userInteractionEnabled = NO;
self.alpha = 0;
if (self.viewController.navigationController) {
[self.viewController.navigationController.view.window addSubview:self];
}
else {
[self.viewController.view.window addSubview:self];
}
_imageView.frame = _startRect;
[UIView animateWithDuration:.2f
animations:^{
self.alpha = 1;
_imageView.frame = _bgView.bounds;
}
completion:^(BOOL finished) {
self.userInteractionEnabled = YES;
}];
}
#pragma mark - Save Call Back
- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
[IBTLoadingView hideHUDWithText:@"已保存"];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// _imageView.center = (CGPoint){
// .x = CGRectGetWidth(_scrollView.bounds) * .5f ,
// .y = CGRectGetHeight(_scrollView.bounds) * .5f,
// };
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;
}
@end