ZLPhotoBrowser.m
7.95 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
//
// ZLPhotoBrowser.m
// 多选相册照片
//
// Created by long on 15/11/27.
// Copyright © 2015年 long. All rights reserved.
//
#import "ZLPhotoBrowser.h"
#import "ZLPhotoBrowserCell.h"
#import "ZLPhotoManager.h"
#import "ZLPhotoModel.h"
#import "ZLThumbnailViewController.h"
#import "SDWebImageManager.h"
@implementation ZLImageNavigationController
- (void)dealloc
{
[[SDWebImageManager sharedManager] cancelAll];
// NSLog(@"---- %s", __FUNCTION__);
}
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self) {
self.navigationBar.barStyle = UIBarStyleBlack;
self.navigationBar.translucent = YES;
}
return self;
}
- (NSMutableArray<ZLPhotoModel *> *)arrSelectedModels
{
if (!_arrSelectedModels) {
_arrSelectedModels = [NSMutableArray array];
}
return _arrSelectedModels;
}
- (void)setConfiguration:(ZLPhotoConfiguration *)configuration
{
_configuration = configuration;
[UIApplication sharedApplication].statusBarStyle = self.configuration.statusBarStyle;
[self.navigationBar setBackgroundImage:[self imageWithColor:configuration.navBarColor] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTintColor:configuration.navTitleColor];
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: configuration.navTitleColor}];
}
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
//- (void)viewWillAppear:(BOOL)animated
//{
// [super viewWillAppear:animated];
// [UIApplication sharedApplication].statusBarStyle = self.configuration.statusBarStyle;
//}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[UIApplication sharedApplication].statusBarStyle = self.previousStatusBarStyle;
// [self setNeedsStatusBarAppearanceUpdate];
}
//BOOL dismiss = NO;
//- (UIStatusBarStyle)previousStatusBarStyle
//{
// if (!dismiss) {
// return UIStatusBarStyleLightContent;
// } else {
// return self.previousStatusBarStyle;
// }
//}
@end
@interface ZLPhotoBrowser ()
@property (nonatomic, strong) NSMutableArray<ZLAlbumListModel *> *arrayDataSources;
@property (nonatomic, strong) UIView *placeholderView;
@end
@implementation ZLPhotoBrowser
- (void)dealloc
{
// NSLog(@"---- %s", __FUNCTION__);
}
- (UIView *)placeholderView
{
if (!_placeholderView) {
_placeholderView = [[UIView alloc] initWithFrame:self.view.bounds];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
imageView.image = GetImageWithName(@"defaultphoto");
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.center = CGPointMake(kViewWidth/2, kViewHeight/2-90);
[_placeholderView addSubview:imageView];
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kViewHeight/2-40, kViewWidth, 20)];
placeholderLabel.text = GetLocalLanguageTextValue(ZLPhotoBrowserNoPhotoText);
placeholderLabel.textAlignment = NSTextAlignmentCenter;
placeholderLabel.textColor = [UIColor darkGrayColor];
placeholderLabel.font = [UIFont systemFontOfSize:15];
[_placeholderView addSubview:placeholderLabel];
_placeholderView.hidden = YES;
[self.view addSubview:_placeholderView];
}
return _placeholderView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeTop;
self.title = GetLocalLanguageTextValue(ZLPhotoBrowserPhotoText);
self.tableView.tableFooterView = [[UIView alloc] init];
// self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:GetLocalLanguageTextValue(ZLPhotoBrowserBackText) style:UIBarButtonItemStylePlain target:nil action:nil];
[self initNavBtn];
if (@available(iOS 11.0, *)) {
[self.tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentAlways];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ZLPhotoConfiguration *configuration = [(ZLImageNavigationController *)self.navigationController configuration];
zl_weakify(self);
[ZLPhotoManager getPhotoAblumList:configuration.allowSelectVideo allowSelectImage:configuration.allowSelectImage complete:^(NSArray<ZLAlbumListModel *> *albums) {
zl_strongify(weakSelf);
strongSelf.arrayDataSources = [NSMutableArray arrayWithArray:albums];
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.tableView reloadData];
});
}];
});
}
- (void)initNavBtn
{
ZLPhotoConfiguration *configuration = [(ZLImageNavigationController *)self.navigationController configuration];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat width = GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 16, YES, 44);
btn.frame = CGRectMake(0, 0, width, 44);
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCancelText) forState:UIControlStateNormal];
[btn setTitleColor:configuration.navTitleColor forState:UIControlStateNormal];
[btn addTarget:self action:@selector(navRightBtn_Click) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)navRightBtn_Click
{
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
if (nav.cancelBlock) {
nav.cancelBlock();
}
[nav dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.arrayDataSources.count == 0) {
self.placeholderView.hidden = NO;
} else {
self.placeholderView.hidden = YES;
}
return self.arrayDataSources.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ZLPhotoBrowserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLPhotoBrowserCell"];
if (!cell) {
cell = [[kZLPhotoBrowserBundle loadNibNamed:@"ZLPhotoBrowserCell" owner:self options:nil] lastObject];
}
ZLAlbumListModel *albumModel = self.arrayDataSources[indexPath.row];
ZLPhotoConfiguration *configuration = [(ZLImageNavigationController *)self.navigationController configuration];
cell.cornerRadio = configuration.cellCornerRadio;
cell.model = albumModel;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self pushThumbnailVCWithIndex:indexPath.row animated:YES];
}
- (void)pushThumbnailVCWithIndex:(NSInteger)index animated:(BOOL)animated
{
ZLAlbumListModel *model = self.arrayDataSources[index];
ZLThumbnailViewController *tvc = [[ZLThumbnailViewController alloc] init];
tvc.albumListModel = model;
[self.navigationController showViewController:tvc sender:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end