TZPhotoPreviewController.m
40.1 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
//
// TZPhotoPreviewController.m
// TZImagePickerController
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
//
#import "TZPhotoPreviewController.h"
#import "TZPhotoPreviewCell.h"
#import "TZAssetModel.h"
#import "UIView+Layout.h"
#import "TZImagePickerController.h"
#import "TZImageManager.h"
#import "TZImageCropManager.h"
#import <AssetsLibrary/AssetsLibrary.h>///lxg
#import "CNEditVideoController.h"
@interface TZPhotoPreviewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate> {
UICollectionView *_collectionView;
UICollectionViewFlowLayout *_layout;
NSArray *_photosTemp;
NSArray *_assetsTemp;
UIView *_naviBar;
UIButton *_backButton;
UIButton *_selectButton;
UILabel *_indexLabel;
UIView *_navBarLine;///LXG线
UIView *_tipBar;///LXG添加提示视图
UILabel *_tipContentLabel;///LXG添加提示内容
UIView *_toolBar;
UIButton *_doneButton;
UIImageView *_numberImageView;
UILabel *_numberLabel;
UIButton *_originalPhotoButton;
UILabel *_originalPhotoLabel;
UILabel *_editTipLable;//LXG底部提示
UIButton *_editTipButton;//LXG编辑按钮
CGFloat _offsetItemCount;
BOOL _didSetIsSelectOriginalPhoto;
}
@property (nonatomic, assign) BOOL isHideNaviBar;
@property (nonatomic, strong) UIView *cropBgView;
@property (nonatomic, strong) UIView *cropView;
@property (nonatomic, assign) double progress;
@property (strong, nonatomic) id alertView;
@end
@implementation TZPhotoPreviewController
- (void)viewDidLoad {
[super viewDidLoad];
[TZImageManager manager].shouldFixOrientation = YES;
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (!_didSetIsSelectOriginalPhoto) {
_isSelectOriginalPhoto = _tzImagePickerVc.isSelectOriginalPhoto;
}
if (!self.models.count) {
self.models = [NSMutableArray arrayWithArray:_tzImagePickerVc.selectedModels];
_assetsTemp = [NSMutableArray arrayWithArray:_tzImagePickerVc.selectedAssets];
}
[self configCollectionView];
[self configCustomNaviBar];
[self configCustomTipBar];///lxg 添加提示
[self configBottomToolBar];
self.view.clipsToBounds = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarOrientationNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)setIsSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
_isSelectOriginalPhoto = isSelectOriginalPhoto;
_didSetIsSelectOriginalPhoto = YES;
}
- (void)setPhotos:(NSMutableArray *)photos {
_photos = photos;
_photosTemp = [NSArray arrayWithArray:photos];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
if (iOS7Later) [UIApplication sharedApplication].statusBarHidden = YES;
if (_currentIndex) [_collectionView setContentOffset:CGPointMake((self.view.tz_width + 20) * _currentIndex, 0) animated:NO];
[self refreshNaviBarAndBottomBarState];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.needShowStatusBar && iOS7Later) {
[UIApplication sharedApplication].statusBarHidden = NO;
}
[TZImageManager manager].shouldFixOrientation = NO;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)configCustomNaviBar {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
_naviBar = [[UIView alloc] initWithFrame:CGRectZero];
_naviBar.backgroundColor = [UIColor colorWithRed:(34/255.0) green:(34/255.0) blue:(34/255.0) alpha:0.7];
_backButton = [[UIButton alloc] initWithFrame:CGRectZero];
[_backButton setImage:[UIImage imageNamedFromMyBundle:@"navi_back"] forState:UIControlStateNormal];
[_backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
_selectButton = [[UIButton alloc] initWithFrame:CGRectZero];
[_selectButton setImage:tzImagePickerVc.photoDefImage forState:UIControlStateNormal];
[_selectButton setImage:tzImagePickerVc.photoSelImage forState:UIControlStateSelected];
_selectButton.imageView.clipsToBounds = YES;
_selectButton.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 0);
_selectButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[_selectButton addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside];
_selectButton.hidden = !tzImagePickerVc.showSelectBtn;
_indexLabel = [[UILabel alloc] init];
_indexLabel.font = [UIFont systemFontOfSize:14];
_indexLabel.textColor = [UIColor whiteColor];
_indexLabel.textAlignment = NSTextAlignmentCenter;
///lxg 导航栏线
_navBarLine = [[UIView alloc]init];
_navBarLine.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1];
_navBarLine.hidden = YES;
[_naviBar addSubview:_selectButton];
[_naviBar addSubview:_indexLabel];
[_naviBar addSubview:_backButton];
[_naviBar addSubview:_navBarLine];///lxg 添加线
[self.view addSubview:_naviBar];
}
/// 添加提示条(LXG)
- (void)configCustomTipBar {
_tipBar = [[UIView alloc] initWithFrame:CGRectZero];
static CGFloat rgb = 34 / 255.0;
_tipBar.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.7];
_tipBar.hidden = YES;
_tipContentLabel = [[UILabel alloc] init];
_tipContentLabel.font = [UIFont systemFontOfSize:15];
_tipContentLabel.textColor = [UIColor whiteColor];
_tipContentLabel.textAlignment = NSTextAlignmentLeft;
_tipContentLabel.backgroundColor = [UIColor clearColor];
[_tipBar addSubview:_tipContentLabel];
[self.view addSubview:_tipBar];
}
- (void)configBottomToolBar {
_toolBar = [[UIView alloc] initWithFrame:CGRectZero];
static CGFloat rgb = 34 / 255.0;
_toolBar.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.7];
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_tzImagePickerVc.allowPickingOriginalPhoto) {
_originalPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
_originalPhotoButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
_originalPhotoButton.backgroundColor = [UIColor clearColor];
[_originalPhotoButton addTarget:self action:@selector(originalPhotoButtonClick) forControlEvents:UIControlEventTouchUpInside];
_originalPhotoButton.titleLabel.font = [UIFont systemFontOfSize:13];
[_originalPhotoButton setTitle:_tzImagePickerVc.fullImageBtnTitleStr forState:UIControlStateNormal];
[_originalPhotoButton setTitle:_tzImagePickerVc.fullImageBtnTitleStr forState:UIControlStateSelected];
[_originalPhotoButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[_originalPhotoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_originalPhotoButton setImage:_tzImagePickerVc.photoPreviewOriginDefImage forState:UIControlStateNormal];
[_originalPhotoButton setImage:_tzImagePickerVc.photoOriginSelImage forState:UIControlStateSelected];
_originalPhotoLabel = [[UILabel alloc] init];
_originalPhotoLabel.textAlignment = NSTextAlignmentLeft;
_originalPhotoLabel.font = [UIFont systemFontOfSize:13];
_originalPhotoLabel.textColor = [UIColor whiteColor];
_originalPhotoLabel.backgroundColor = [UIColor clearColor];
if (_isSelectOriginalPhoto) [self showPhotoBytes];
}
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
_doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:_tzImagePickerVc.doneBtnTitleStr forState:UIControlStateNormal];
[_doneButton setTitleColor:_tzImagePickerVc.oKButtonTitleColorNormal forState:UIControlStateNormal];
_numberImageView = [[UIImageView alloc] initWithImage:_tzImagePickerVc.photoNumberIconImage];
_numberImageView.backgroundColor = [UIColor clearColor];
_numberImageView.clipsToBounds = YES;
_numberImageView.contentMode = UIViewContentModeScaleAspectFit;
_numberImageView.hidden = _tzImagePickerVc.selectedModels.count <= 0;
_numberLabel = [[UILabel alloc] init];
_numberLabel.font = [UIFont systemFontOfSize:15];
_numberLabel.textColor = [UIColor whiteColor];
_numberLabel.textAlignment = NSTextAlignmentCenter;
_numberLabel.text = [NSString stringWithFormat:@"%zd",_tzImagePickerVc.selectedModels.count];
_numberLabel.hidden = _tzImagePickerVc.selectedModels.count <= 0;
_numberLabel.backgroundColor = [UIColor clearColor];
#pragma mark - 添加: 选择图片个数显示(LXG)
_numberImageView.hidden = YES;
_numberLabel.hidden = YES;
NSString *titleStr = _tzImagePickerVc.selectedModels.count > 0 ? [NSString stringWithFormat:@"完成(%zd)",_tzImagePickerVc.selectedModels.count] : @"完成";
[_doneButton setTitle:titleStr forState:UIControlStateNormal];
///LXG 添加视频编辑
_editTipLable = [[UILabel alloc] init];
_editTipLable.font = [UIFont systemFontOfSize:15];
_editTipLable.textAlignment = NSTextAlignmentLeft;
_editTipLable.hidden = YES;
_editTipLable.textColor = [UIColor whiteColor];
_editTipLable.backgroundColor = [UIColor clearColor];
_editTipButton = [UIButton buttonWithType:UIButtonTypeCustom];
_editTipButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_editTipButton addTarget:self action:@selector(editVideoButtonClick) forControlEvents:UIControlEventTouchUpInside];
_editTipButton.hidden = YES;
[_editTipButton setTitle:_tzImagePickerVc.editBtnTitleStr forState:UIControlStateNormal];
[_editTipButton setTitleColor:_tzImagePickerVc.editBtnTitleColor forState:UIControlStateNormal];
[_originalPhotoButton addSubview:_originalPhotoLabel];
[_toolBar addSubview:_doneButton];
[_toolBar addSubview:_originalPhotoButton];
[_toolBar addSubview:_numberImageView];
[_toolBar addSubview:_numberLabel];
[_toolBar addSubview:_editTipLable];///LXG
[_toolBar addSubview:_editTipButton];///LXG
[self.view addSubview:_toolBar];
if (_tzImagePickerVc.photoPreviewPageUIConfigBlock) {
_tzImagePickerVc.photoPreviewPageUIConfigBlock(_collectionView, _naviBar, _backButton, _selectButton, _indexLabel, _toolBar, _originalPhotoButton, _originalPhotoLabel, _doneButton, _numberImageView, _numberLabel);
}
}
- (void)configCollectionView {
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
_collectionView.backgroundColor = [UIColor blackColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
_collectionView.scrollsToTop = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.contentOffset = CGPointMake(0, 0);
_collectionView.contentSize = CGSizeMake(self.models.count * (self.view.tz_width + 20), 0);
[self.view addSubview:_collectionView];
[_collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
[_collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
[_collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
}
- (void)configCropView {
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_tzImagePickerVc.maxImagesCount <= 1 && _tzImagePickerVc.allowCrop) {
[_cropView removeFromSuperview];
[_cropBgView removeFromSuperview];
_cropBgView = [UIView new];
_cropBgView.userInteractionEnabled = NO;
_cropBgView.frame = self.view.bounds;
_cropBgView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_cropBgView];
[TZImageCropManager overlayClippingWithView:_cropBgView cropRect:_tzImagePickerVc.cropRect containerView:self.view needCircleCrop:_tzImagePickerVc.needCircleCrop];
_cropView = [UIView new];
_cropView.userInteractionEnabled = NO;
_cropView.frame = _tzImagePickerVc.cropRect;
_cropView.backgroundColor = [UIColor clearColor];
_cropView.layer.borderColor = [UIColor whiteColor].CGColor;
_cropView.layer.borderWidth = 1.0;
if (_tzImagePickerVc.needCircleCrop) {
_cropView.layer.cornerRadius = _tzImagePickerVc.cropRect.size.width / 2;
_cropView.clipsToBounds = YES;
}
[self.view addSubview:_cropView];
if (_tzImagePickerVc.cropViewSettingBlock) {
_tzImagePickerVc.cropViewSettingBlock(_cropView);
}
[self.view bringSubviewToFront:_naviBar];
[self.view bringSubviewToFront:_tipBar];
[self.view bringSubviewToFront:_toolBar];
}
}
#pragma mark - Layout
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
CGFloat statusBarHeight = [TZCommonTools tz_statusBarHeight];
CGFloat statusBarHeightInterval = statusBarHeight - 20;
CGFloat naviBarHeight = statusBarHeight + _tzImagePickerVc.navigationBar.tz_height;
_naviBar.frame = CGRectMake(0, 0, self.view.tz_width, naviBarHeight);
_backButton.frame = CGRectMake(10, 10 + statusBarHeightInterval, 44, 44);
_selectButton.frame = CGRectMake(self.view.tz_width - 56, 10 + statusBarHeightInterval, 44, 44);
_indexLabel.frame = _selectButton.frame;
_navBarLine.frame = CGRectMake(0, _naviBar.tz_height - 0.5, _naviBar.tz_width, 0.5);
///Lxg 提示视图约束
CGFloat tipBarHeight = _tzImagePickerVc.navigationBar.tz_height;
_tipBar.frame = CGRectMake(0, _naviBar.tz_bottom, _naviBar.tz_width, tipBarHeight);
_tipContentLabel.frame = CGRectMake(12, 0, _tipBar.tz_width - 24, tipBarHeight);
_layout.itemSize = CGSizeMake(self.view.tz_width + 20, self.view.tz_height);
_layout.minimumInteritemSpacing = 0;
_layout.minimumLineSpacing = 0;
_collectionView.frame = CGRectMake(-10, 0, self.view.tz_width + 20, self.view.tz_height);
[_collectionView setCollectionViewLayout:_layout];
if (_offsetItemCount > 0) {
CGFloat offsetX = _offsetItemCount * _layout.itemSize.width;
[_collectionView setContentOffset:CGPointMake(offsetX, 0)];
}
if (_tzImagePickerVc.allowCrop) {
[_collectionView reloadData];
}
CGFloat toolBarHeight = [TZCommonTools tz_isIPhoneX] ? 44 + (83 - 49) : 44;
CGFloat toolBarTop = self.view.tz_height - toolBarHeight;
_toolBar.frame = CGRectMake(0, toolBarTop, self.view.tz_width, toolBarHeight);
if (_tzImagePickerVc.allowPickingOriginalPhoto) {
CGFloat fullImageWidth = [_tzImagePickerVc.fullImageBtnTitleStr tz_calculateSizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} maxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)].width;
_originalPhotoButton.frame = CGRectMake(0, 0, fullImageWidth + 56, 44);
_originalPhotoLabel.frame = CGRectMake(fullImageWidth + 42, 0, 80, 44);
}
[_doneButton sizeToFit];
_doneButton.frame = CGRectMake(self.view.tz_width - _doneButton.tz_width - 12, 0, _doneButton.tz_width, 44);
_numberImageView.frame = CGRectMake(_doneButton.tz_left - 24 - 5, 10, 24, 24);
_numberLabel.frame = _numberImageView.frame;
///LXG
[_editTipButton sizeToFit];
_editTipButton.frame = CGRectMake(self.view.tz_width - 44 - 12, 0, 44, 44);
_editTipLable.frame = CGRectMake(12, 10, self.view.tz_width - 24 - _editTipButton.tz_width, 24);
[self configCropView];
if (_tzImagePickerVc.photoPreviewPageDidLayoutSubviewsBlock) {
_tzImagePickerVc.photoPreviewPageDidLayoutSubviewsBlock(_collectionView, _naviBar, _backButton, _selectButton, _indexLabel, _toolBar, _originalPhotoButton, _originalPhotoLabel, _doneButton, _numberImageView, _numberLabel);
}
}
#pragma mark - Notification
- (void)didChangeStatusBarOrientationNotification:(NSNotification *)noti {
_offsetItemCount = _collectionView.contentOffset.x / _layout.itemSize.width;
}
#pragma mark - Click Event
- (void)select:(UIButton *)selectButton {
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
TZAssetModel *model = _models[_currentIndex];
///LXG:加入当点击选择时 滑动到另一个页面 选中为当前页面判断是否可选
if (!selectButton.enabled) {
return;
}
if (!selectButton.isSelected) {
// 1. select:check if over the maxImagesCount / 选择照片,检查是否超过了最大个数的限制
if (_tzImagePickerVc.selectedModels.count >= _tzImagePickerVc.maxImagesCount) {
NSString *title = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Select a maximum of %zd photos"], _tzImagePickerVc.maxImagesCount];
[_tzImagePickerVc showAlertWithTitle:title];
return;
// 2. if not over the maxImagesCount / 如果没有超过最大个数限制
} else {
[_tzImagePickerVc addSelectedModel:model];
if (self.photos) {
[_tzImagePickerVc.selectedAssets addObject:_assetsTemp[_currentIndex]];
[self.photos addObject:_photosTemp[_currentIndex]];
}
if (model.type == TZAssetModelMediaTypeVideo && !_tzImagePickerVc.allowPickingMultipleVideo) {
[_tzImagePickerVc showAlertWithTitle:[NSBundle tz_localizedStringForKey:@"Select the video when in multi state, we will handle the video as a photo"]];
}
}
} else {
NSArray *selectedModels = [NSArray arrayWithArray:_tzImagePickerVc.selectedModels];
for (TZAssetModel *model_item in selectedModels) {
if ([[[TZImageManager manager] getAssetIdentifier:model.asset] isEqualToString:[[TZImageManager manager] getAssetIdentifier:model_item.asset]]) {
// 1.6.7版本更新:防止有多个一样的model,一次性被移除了
NSArray *selectedModelsTmp = [NSArray arrayWithArray:_tzImagePickerVc.selectedModels];
for (NSInteger i = 0; i < selectedModelsTmp.count; i++) {
TZAssetModel *model = selectedModelsTmp[i];
if ([model isEqual:model_item]) {
[_tzImagePickerVc removeSelectedModel:model];
// [_tzImagePickerVc.selectedModels removeObjectAtIndex:i];
break;
}
}
if (self.photos) {
// 1.6.7版本更新:防止有多个一样的asset,一次性被移除了
NSArray *selectedAssetsTmp = [NSArray arrayWithArray:_tzImagePickerVc.selectedAssets];
for (NSInteger i = 0; i < selectedAssetsTmp.count; i++) {
id asset = selectedAssetsTmp[i];
if ([asset isEqual:_assetsTemp[_currentIndex]]) {
[_tzImagePickerVc.selectedAssets removeObjectAtIndex:i];
break;
}
}
// [_tzImagePickerVc.selectedAssets removeObject:_assetsTemp[_currentIndex]];
[self.photos removeObject:_photosTemp[_currentIndex]];
}
break;
}
}
}
model.isSelected = !selectButton.isSelected;
[self refreshNaviBarAndBottomBarState];
if (model.isSelected) {
[UIView showOscillatoryAnimationWithLayer:selectButton.imageView.layer type:TZOscillatoryAnimationToBigger];
}
[UIView showOscillatoryAnimationWithLayer:_numberImageView.layer type:TZOscillatoryAnimationToSmaller];
}
- (void)backButtonClick {
if (self.navigationController.childViewControllers.count < 2) {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
if ([self.navigationController isKindOfClass: [TZImagePickerController class]]) {
TZImagePickerController *nav = (TZImagePickerController *)self.navigationController;
if (nav.imagePickerControllerDidCancelHandle) {
nav.imagePickerControllerDidCancelHandle();
}
}
return;
}
[self.navigationController popViewControllerAnimated:YES];
if (self.backButtonClickBlock) {
self.backButtonClickBlock(_isSelectOriginalPhoto);
}
}
- (void)doneButtonClick {
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
// 如果图片正在从iCloud同步中,提醒用户
if (_progress > 0 && _progress < 1 && (_selectButton.isSelected || !_tzImagePickerVc.selectedModels.count )) {
_alertView = [_tzImagePickerVc showAlertWithTitle:[NSBundle tz_localizedStringForKey:@"Synchronizing photos from iCloud"]];
return;
}
// 如果没有选中过照片 点击确定时选中当前预览的照片
if (_tzImagePickerVc.selectedModels.count == 0 && _tzImagePickerVc.minImagesCount <= 0) {
TZAssetModel *model = _models[_currentIndex];
[_tzImagePickerVc addSelectedModel:model];
}
if (_tzImagePickerVc.allowCrop) { // 裁剪状态
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_currentIndex inSection:0];
TZPhotoPreviewCell *cell = (TZPhotoPreviewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
UIImage *cropedImage = [TZImageCropManager cropImageView:cell.previewView.imageView toRect:_tzImagePickerVc.cropRect zoomScale:cell.previewView.scrollView.zoomScale containerView:self.view];
if (_tzImagePickerVc.needCircleCrop) {
cropedImage = [TZImageCropManager circularClipImage:cropedImage];
}
if (self.doneButtonClickBlockCropMode) {
TZAssetModel *model = _models[_currentIndex];
self.doneButtonClickBlockCropMode(cropedImage,model.asset);
}
} else if (self.doneButtonClickBlock) { // 非裁剪状态
self.doneButtonClickBlock(_isSelectOriginalPhoto);
}
if (self.doneButtonClickBlockWithPreviewType) {
self.doneButtonClickBlockWithPreviewType(self.photos,_tzImagePickerVc.selectedAssets,self.isSelectOriginalPhoto);
}
}
///lxg 编辑按钮实现
- (void)editVideoButtonClick{
NSLog(@"开始编辑视频了");
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
CNEditVideoController *editVideoVC = [[CNEditVideoController alloc]init];
TZAssetModel *model = _models[_currentIndex];
editVideoVC.model = model;
editVideoVC.models = _models;
editVideoVC.currentIndex = _currentIndex;
editVideoVC.isCropImage = _isCropImage;
__weak typeof(self) weakSelf = self;
editVideoVC.doneButtonEditClickBlock = ^(BOOL isSelectOriginalPhoto) {
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.doneButtonClickBlock(isSelectOriginalPhoto);
};
[_tzImagePickerVc pushViewController:editVideoVC animated:NO];
}
- (void)originalPhotoButtonClick {
_originalPhotoButton.selected = !_originalPhotoButton.isSelected;
_isSelectOriginalPhoto = _originalPhotoButton.isSelected;
_originalPhotoLabel.hidden = !_originalPhotoButton.isSelected;
if (_isSelectOriginalPhoto) {
[self showPhotoBytes];
if (!_selectButton.isSelected) {
// 如果当前已选择照片张数 < 最大可选张数 && 最大可选张数大于1,就选中该张图
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_tzImagePickerVc.selectedModels.count < _tzImagePickerVc.maxImagesCount && _tzImagePickerVc.showSelectBtn) {
[self select:_selectButton];
}
}
}
}
- (void)didTapPreviewCell {
self.isHideNaviBar = !self.isHideNaviBar;
_naviBar.hidden = self.isHideNaviBar;
_toolBar.hidden = self.isHideNaviBar;
if (_naviBar.hidden && _toolBar.hidden) {
_tipBar.hidden = YES;
}else{
[self refreshNaviBarAndBottomBarState];
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offSetWidth = scrollView.contentOffset.x;
offSetWidth = offSetWidth + ((self.view.tz_width + 20) * 0.5);
NSInteger currentIndex = offSetWidth / (self.view.tz_width + 20);
if (currentIndex < _models.count && _currentIndex != currentIndex) {
_currentIndex = currentIndex;
[self refreshNaviBarAndBottomBarState];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
}
#pragma mark - UICollectionViewDataSource && Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _models.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
TZAssetModel *model = _models[indexPath.item];
TZAssetPreviewCell *cell;
__weak typeof(self) weakSelf = self;
#pragma mark - 更改视频,GIF,显示
if (model.type == TZAssetModelMediaTypeVideo) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
} else if (model.type == TZAssetModelMediaTypePhotoGif && _tzImagePickerVc.allowPickingGif) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZPhotoPreviewCell" forIndexPath:indexPath];
TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
__weak typeof(_tzImagePickerVc) weakTzImagePickerVc = _tzImagePickerVc;
__weak typeof(_collectionView) weakCollectionView = _collectionView;
__weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
[photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
__strong typeof(weakSelf) strongSelf = weakSelf;
__strong typeof(weakTzImagePickerVc) strongTzImagePickerVc = weakTzImagePickerVc;
__strong typeof(weakCollectionView) strongCollectionView = weakCollectionView;
__strong typeof(weakCell) strongCell = weakCell;
strongSelf.progress = progress;
if (progress >= 1) {
if (strongSelf.isSelectOriginalPhoto) [strongSelf showPhotoBytes];
if (strongSelf.alertView && [strongCollectionView.visibleCells containsObject:strongCell]) {
[strongTzImagePickerVc hideAlertView:strongSelf.alertView];
strongSelf.alertView = nil;
[strongSelf doneButtonClick];
}
}
}];
}
cell.model = model;
[cell setSingleTapGestureBlock:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf didTapPreviewCell];
}];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[TZPhotoPreviewCell class]]) {
[(TZPhotoPreviewCell *)cell recoverSubviews];
}
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[TZPhotoPreviewCell class]]) {
[(TZPhotoPreviewCell *)cell recoverSubviews];
} else if ([cell isKindOfClass:[TZVideoPreviewCell class]]) {
[(TZVideoPreviewCell *)cell pausePlayerAndShowNaviBar];
}
}
#pragma mark - Private Method
- (void)dealloc {
// NSLog(@"%@ dealloc",NSStringFromClass(self.class));
}
- (void)refreshNaviBarAndBottomBarState {
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
TZAssetModel *model = _models[_currentIndex];
_selectButton.selected = model.isSelected;
[self refreshSelectButtonImageViewContentMode];
if (_selectButton.isSelected && _tzImagePickerVc.showSelectedIndex && _tzImagePickerVc.showSelectBtn) {
NSString *assetId = [[TZImageManager manager] getAssetIdentifier:model.asset];
NSString *index = [NSString stringWithFormat:@"%zd", [_tzImagePickerVc.selectedAssetIds indexOfObject:assetId] + 1];
_indexLabel.text = index;
_indexLabel.hidden = NO;
} else {
_indexLabel.hidden = YES;
}
_numberLabel.text = [NSString stringWithFormat:@"%zd",_tzImagePickerVc.selectedModels.count];
_numberImageView.hidden = (_tzImagePickerVc.selectedModels.count <= 0 || _isHideNaviBar || _isCropImage);
_numberLabel.hidden = (_tzImagePickerVc.selectedModels.count <= 0 || _isHideNaviBar || _isCropImage);
#pragma mark - 添加: 选择图片个数显示(LXG)
//隐藏图标
_numberLabel.hidden = YES;
_numberImageView.hidden = YES;
NSString *titleStr = _tzImagePickerVc.selectedModels.count > 0 ? [NSString stringWithFormat:@"完成(%zd)",_tzImagePickerVc.selectedModels.count] : @"完成";
[_doneButton setTitle:titleStr forState:UIControlStateNormal];
_originalPhotoButton.selected = _isSelectOriginalPhoto;
_originalPhotoLabel.hidden = !_originalPhotoButton.isSelected;
if (_isSelectOriginalPhoto) [self showPhotoBytes];
// If is previewing video, hide original photo button
// 如果正在预览的是视频,隐藏原图按钮
if (!_isHideNaviBar) {
if (model.type == TZAssetModelMediaTypeVideo) {
_originalPhotoButton.hidden = YES;
_originalPhotoLabel.hidden = YES;
} else {
_originalPhotoButton.hidden = NO;
if (_isSelectOriginalPhoto) _originalPhotoLabel.hidden = NO;
}
}
_doneButton.hidden = NO;
_selectButton.hidden = !_tzImagePickerVc.showSelectBtn;
// 让宽度/高度小于 最小可选照片尺寸 的图片不能选中
if (![[TZImageManager manager] isPhotoSelectableWithAsset:model.asset]) {
_numberLabel.hidden = YES;
_numberImageView.hidden = YES;
_selectButton.hidden = YES;
_originalPhotoButton.hidden = YES;
_originalPhotoLabel.hidden = YES;
_doneButton.hidden = YES;
}
#pragma mark - 添加: 视频显示操作(LXG)
//多选状态下**不允许**图片和视频混选
if (!_tzImagePickerVc.allowPickingMultipleVideo) {
if (model.type == TZAssetModelMediaTypeVideo) {//多选状态下**不允许**图片和视频混选 -> 视频
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_tzImagePickerVc.selectedModels.count > 0) {//当选择图片大于0时
TZAssetModel *model = _tzImagePickerVc.selectedModels[0];
if (model.type == TZAssetModelMediaTypePhoto
|| model.type == TZAssetModelMediaTypePhotoGif
||model.type == TZAssetModelMediaTypeLivePhoto) {//选择为图片时
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_navBarLine.hidden = NO;
_tipBar.hidden = NO;
_tipContentLabel.text = @"选择照片时不能选择视频";
_doneButton.hidden = NO;
_doneButton.enabled = YES;
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}else{//未来可能是语音
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_navBarLine.hidden = NO;
_tipBar.hidden = NO;
_tipContentLabel.text = @"选择照片时不能选择音频";
_doneButton.hidden = NO;
_doneButton.enabled = YES;
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}
}else{//当选择图片==0时
NSTimeInterval duration = 0.0;
if ([model.asset isKindOfClass:[PHAsset class]]) {
PHAsset *asset = model.asset;
duration = asset.duration;
}else if ([model.asset isKindOfClass:[ALAsset class]]){
duration = [[model.asset valueForProperty:ALAssetPropertyDuration] doubleValue];
}
if (duration > _tzImagePickerVc.maxVideoDuration) {//视频长度>最大限制长度
NSInteger minutes = _tzImagePickerVc.maxVideoDuration / 60;
NSInteger seconds = _tzImagePickerVc.maxVideoDuration % 60;
NSString *timeStr = [NSString string];
if (minutes == 0) {
timeStr = [NSString stringWithFormat:@"不能分享超过%li秒视频",(long)seconds];
}else if (seconds == 0){
timeStr = [NSString stringWithFormat:@"不能分享超过%li分视频",(long)minutes];
}else{
timeStr = [NSString stringWithFormat:@"不能分享超过%li分%li秒视频",(long)minutes,(long)seconds];
}
_tipContentLabel.text = timeStr;
//当选择图片==0时,视频长度>最大限制长度
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_navBarLine.hidden = NO;
_tipBar.hidden = NO;
_tipContentLabel.text = timeStr;
_doneButton.hidden = NO;
if (_tzImagePickerVc.selectedModels.count > 0) {
_doneButton.enabled = YES;
}else{
_doneButton.enabled = NO;
}
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}else if(duration <= _tzImagePickerVc.maxVideoDuration && duration > _tzImagePickerVc.maxEditVideoTime){//视频长度<=最大限制长度,视频长度>=最小限制长度
NSInteger minutes = _tzImagePickerVc.maxEditVideoTime / 60;
NSInteger seconds = _tzImagePickerVc.maxEditVideoTime % 60;
NSString *timeStr = [NSString string];
if (minutes == 0) {
timeStr = [NSString stringWithFormat:@"朋友圈只能分享%li秒限制的视频,需进行编辑",(long)seconds];
}else if (seconds == 0){
timeStr = [NSString stringWithFormat:@"朋友圈只能分享%li分限制的视频,需进行编辑",(long)minutes];
}else{
timeStr = [NSString stringWithFormat:@"朋友圈只能分享%li分%li秒限制的视频,需进行编辑",(long)minutes,(long)seconds];
}
_editTipLable.text = timeStr;
//当选择图片==0时,视频长度<=最大限制长度,视频长度>最大分享长度
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_navBarLine.hidden = YES;
_tipBar.hidden = YES;
_tipContentLabel.text = timeStr;
_doneButton.hidden = YES;
_doneButton.enabled = YES;
_editTipButton.hidden = NO;
_editTipLable.hidden = NO;
}else{//视频长度<=最大分享长度
//当选择图片==0时,视频长度<=最大分享长度
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_navBarLine.hidden = YES;
_tipBar.hidden = YES;
_doneButton.hidden = NO;
_doneButton.enabled = YES;
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}
}
} else {//滑动到不是视频时
//滑动到不是视频时
_selectButton.hidden = NO;
_selectButton.enabled = YES;
_navBarLine.hidden = YES;
_tipBar.hidden = YES;
_doneButton.hidden = NO;
_doneButton.enabled = YES;
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}
}else{///多选状态下**允许**图片和视频混选(lxg)
NSTimeInterval duration = 0.0;
if ([model.asset isKindOfClass:[PHAsset class]]) {
PHAsset *asset = model.asset;
duration = asset.duration;
}else if([model.asset isKindOfClass:[ALAsset class]]) {
duration = [[model.asset valueForProperty:ALAssetPropertyDuration] doubleValue];
}
if (model.type == TZAssetModelMediaTypeVideo
&& duration > _tzImagePickerVc.maxVideoDuration) {//视频,支持混选,时长超过最大时长限制
NSInteger minutes = _tzImagePickerVc.maxVideoDuration / 60;
NSInteger seconds = _tzImagePickerVc.maxVideoDuration % 60;
NSString *timeStr = [NSString string];
if (minutes == 0) {
timeStr = [NSString stringWithFormat:@"不能选择超过%li秒视频",(long)seconds];
}else if (seconds == 0){
timeStr = [NSString stringWithFormat:@"不能选择超过%li分视频",(long)minutes];
}else{
timeStr = [NSString stringWithFormat:@"不能选择超过%li分%li秒视频",(long)minutes,(long)seconds];
}
_selectButton.hidden = YES;
_selectButton.enabled = NO;
_selectButton.enabled = NO;
_navBarLine.hidden = NO;
_tipBar.hidden = NO;
_tipContentLabel.text = timeStr;
_doneButton.hidden = NO;
if (_tzImagePickerVc.selectedModels.count > 0) {
_doneButton.enabled = YES;
}else{
_doneButton.enabled = NO;
}
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}else{///多选状态下**允许**图片和视频混选(lxg) - 不是视频 || 视频<= _tzImagePickerVc.maxVideoDuration 最大时长限制
_selectButton.hidden = NO;
_selectButton.enabled = YES;
_navBarLine.hidden = YES;
_tipBar.hidden = YES;
_doneButton.hidden = NO;
_doneButton.enabled = YES;
_editTipButton.hidden = YES;
_editTipLable.hidden = YES;
}
}
}
- (void)refreshSelectButtonImageViewContentMode {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self->_selectButton.imageView.image.size.width <= 27) {
self->_selectButton.imageView.contentMode = UIViewContentModeCenter;
} else {
self->_selectButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
});
}
- (void)showPhotoBytes {
[[TZImageManager manager] getPhotosBytesWithArray:@[_models[_currentIndex]] completion:^(NSString *totalBytes) {
self->_originalPhotoLabel.text = [NSString stringWithFormat:@"(%@)",totalBytes];
}];
}
@end