PhotoPreviewViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // PhotoPreviewViewController.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/11.
  6. //
  7. #import "PhotoPreviewViewController.h"
  8. #import "TZPhotoPreviewCell.h"
  9. #import "photoPreViewBottomView.h"
  10. @interface PhotoPreviewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
  11. @property (strong, nonatomic) UIButton *selectButton;
  12. @property (strong, nonatomic) UICollectionView *photoPreviewCollectionV;
  13. @property (strong, nonatomic) photoPreViewBottomView *photoPreViewBottomV;
  14. @property (assign, nonatomic) BOOL canSetCurrentIndex;//
  15. @end
  16. @implementation PhotoPreviewViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. [self.view setBackgroundColor:[UIColor blackColor]];
  21. [self.toolBar setHidden:YES];
  22. [self.navigationBar setHidden:YES];
  23. [self.navBarBGView setHidden:NO];
  24. [self setupPhotoPreviewNavBarView];
  25. //列表view
  26. [self setupPhotoPreviewCollectionView];
  27. [self setupPhotoPreViewBottomView];
  28. }
  29. - (void)viewWillAppear:(BOOL)animated
  30. {
  31. [super viewWillAppear: animated];
  32. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  33. //[self.photoPreviewCollectionV reloadData];
  34. if (_currentIndex) {
  35. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  36. self->_canSetCurrentIndex = YES;
  37. [self.photoPreviewCollectionV setContentOffset:CGPointMake(SCREEN_W * self.currentIndex, 0) animated:NO];
  38. });
  39. }
  40. [self refreshNaviBarAndBottomBarState];
  41. }
  42. - (void)viewWillDisappear:(BOOL)animated
  43. {
  44. [super viewWillDisappear:animated];
  45. ///黑色
  46. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  47. }
  48. #pragma mark 点击选中按钮
  49. - (void)didClickButtonFun:(UIButton*)but
  50. {
  51. but.selected = !but.selected;
  52. [self handlCellSelectFun];
  53. }
  54. #pragma mark 处理点击选中相关
  55. - (void)handlCellSelectFun
  56. {
  57. TZAssetModel *model = self.assets[_currentIndex];
  58. //超出最大限制
  59. if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection ) {
  60. return;
  61. }
  62. //取消选中
  63. if ([self.indexPathsForSelectedItems containsObject:model]) {
  64. [self.indexPathsForSelectedItems removeObject:model];
  65. model.isSelected = NO;
  66. }
  67. else{//选中
  68. [self.indexPathsForSelectedItems addObject:model];
  69. model.isSelected = YES;
  70. if(!model.imageData)
  71. {
  72. [[PHImageManager defaultManager] requestImageDataForAsset:model.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  73. // 直接得到最终的 NSData 数据
  74. if (imageData) {
  75. model.imageData = imageData;
  76. }
  77. [self setDataToBottomViewFun];
  78. }];
  79. }
  80. }
  81. [self setDataToBottomViewFun];
  82. //[self refreshNaviBarAndBottomBarState];
  83. }
  84. #pragma mark 同步数据到底部
  85. - (void)setDataToBottomViewFun
  86. {
  87. self.photoPreViewBottomV.indexPathsForSelectedItems = self.indexPathsForSelectedItems;
  88. }
  89. #pragma mark 刷新导航栏的UI
  90. - (void)refreshNaviBarAndBottomBarState
  91. {
  92. self.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",(_currentIndex+1),_assets.count];
  93. TZAssetModel *model = self.assets[_currentIndex];
  94. self.selectButton.selected = model.isSelected;
  95. }
  96. - (void)setupPhotoPreviewNavBarView
  97. {
  98. [self.backBtn setImage:[UIImage imageNamed:@"icon_white_back"] forState:UIControlStateNormal];
  99. self.navBarBGView.backgroundColor = [UIColor blackColor];
  100. self.titleLabel.textColor = [UIColor whiteColor];
  101. UIButton *but = [[UIButton alloc] init];
  102. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  103. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  104. [but addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  105. [self.navBarBGView addSubview:but];
  106. self.selectButton = but;
  107. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.right.mas_equalTo(-15);
  109. make.width.mas_equalTo(40);
  110. make.height.mas_equalTo(40);
  111. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0.f);
  112. }];
  113. }
  114. - (void)setupPhotoPreviewCollectionView
  115. {
  116. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  117. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  118. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  119. collectionView.dataSource = self;
  120. collectionView.delegate = self;
  121. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  122. collectionView.pagingEnabled = YES;
  123. collectionView.showsVerticalScrollIndicator = NO;
  124. collectionView.showsHorizontalScrollIndicator = NO;
  125. [self.view insertSubview:collectionView atIndex:0];
  126. collectionView.backgroundColor = [UIColor blackColor];
  127. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
  128. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCellGIF"];
  129. [collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
  130. [collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
  131. self.photoPreviewCollectionV = collectionView;
  132. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.left.mas_equalTo(0);
  134. make.right.mas_equalTo(0);
  135. make.bottom.mas_equalTo(-(60 + safeArea));
  136. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  137. }];
  138. }
  139. - (void)setupPhotoPreViewBottomView
  140. {
  141. photoPreViewBottomView * bottomView = [[photoPreViewBottomView alloc] init];
  142. [self.view addSubview:bottomView];
  143. self.photoPreViewBottomV = bottomView;
  144. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  145. make.left.mas_equalTo(0);
  146. make.right.mas_equalTo(0);
  147. make.bottom.mas_equalTo(0);
  148. make.height.mas_equalTo((60 + safeArea));
  149. }];
  150. }
  151. #pragma mark - UIScrollViewDelegate
  152. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  153. CGFloat offSetWidth = scrollView.contentOffset.x;
  154. offSetWidth = offSetWidth + (SCREEN_W * 0.5);
  155. NSInteger currentIndex = offSetWidth / (SCREEN_W + 20);
  156. if (currentIndex < _assets.count && _currentIndex != currentIndex && _canSetCurrentIndex) {
  157. _currentIndex = currentIndex;
  158. [self refreshNaviBarAndBottomBarState];
  159. }
  160. [[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
  161. }
  162. #pragma mark - uicollectionDelegate
  163. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  164. return self.assets.count;
  165. }
  166. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  167. TZAssetModel *model = self.assets[indexPath.row];
  168. TZAssetPreviewCell *cell;
  169. __weak typeof(self) weakSelf = self;
  170. if (model.type == TZAssetModelMediaTypeVideo) {
  171. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
  172. TZVideoPreviewCell *currentCell = (TZVideoPreviewCell *)cell;
  173. currentCell.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  174. model.iCloudFailed = isSyncFailed;
  175. //[weakSelf didICloudSyncStatusChanged:model];
  176. };
  177. } else if (model.type == TZAssetModelMediaTypePhotoGif) {
  178. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
  179. TZGifPreviewCell *currentCell = (TZGifPreviewCell *)cell;
  180. currentCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  181. model.iCloudFailed = isSyncFailed;
  182. //[weakSelf didICloudSyncStatusChanged:model];
  183. };
  184. } else {
  185. NSString *reuseId = model.type == TZAssetModelMediaTypePhotoGif ? @"TZPhotoPreviewCellGIF" : @"TZPhotoPreviewCell";
  186. cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
  187. TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
  188. //photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
  189. //photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
  190. //photoPreviewCell.scaleAspectFillCrop = _tzImagePickerVc.scaleAspectFillCrop;
  191. // __weak typeof(_collectionView) weakCollectionView = _collectionView;
  192. // __weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
  193. // [photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
  194. // __strong typeof(weakSelf) strongSelf = weakSelf;
  195. // __strong typeof(weakCollectionView) strongCollectionView = weakCollectionView;
  196. // __strong typeof(weakCell) strongCell = weakCell;
  197. // strongSelf.progress = progress;
  198. // if (progress >= 1) {
  199. // if (strongSelf.isSelectOriginalPhoto) [strongSelf showPhotoBytes];
  200. // if (strongSelf.alertView && [strongCollectionView.visibleCells containsObject:strongCell]) {
  201. // [strongSelf.alertView dismissViewControllerAnimated:YES completion:^{
  202. // strongSelf.alertView = nil;
  203. // [strongSelf doneButtonClick];
  204. // }];
  205. // }
  206. // }
  207. // }];
  208. photoPreviewCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  209. model.iCloudFailed = isSyncFailed;
  210. //[weakSelf didICloudSyncStatusChanged:model];
  211. };
  212. }
  213. cell.model = model;
  214. [cell setSingleTapGestureBlock:^{
  215. __strong typeof(weakSelf) strongSelf = weakSelf;
  216. //[strongSelf didTapPreviewCell];
  217. }];
  218. return cell;
  219. }
  220. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  221. return UIEdgeInsetsMake(0, 0, 0, 0);
  222. }
  223. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  224. CGFloat wh = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - (60 + safeArea);
  225. return CGSizeMake(SCREEN_W, wh);
  226. }
  227. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  228. return 0.0;
  229. }
  230. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  231. return 0.0;
  232. }
  233. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  234. }
  235. @end