PhotoPreviewViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. }
  53. #pragma mark 处理点击选中相关
  54. - (void)handlCellSelectFun
  55. {
  56. TZAssetModel *model = self.assets[_currentIndex];
  57. //超出最大限制
  58. if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection ) {
  59. return;
  60. }
  61. //取消选中
  62. if ([self.indexPathsForSelectedItems containsObject:model]) {
  63. [self.indexPathsForSelectedItems removeObject:model];
  64. model.isSelected = NO;
  65. }
  66. else{//选中
  67. [self.indexPathsForSelectedItems addObject:model];
  68. model.isSelected = NO;
  69. if(!model.imageData)
  70. {
  71. [[PHImageManager defaultManager] requestImageDataForAsset:model.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  72. // 直接得到最终的 NSData 数据
  73. if (imageData) {
  74. model.imageData = imageData;
  75. }
  76. [self setDataToBottomViewFun];
  77. }];
  78. }
  79. }
  80. [self setDataToBottomViewFun];
  81. [self refreshNaviBarAndBottomBarState];
  82. }
  83. #pragma mark 同步数据到底部
  84. - (void)setDataToBottomViewFun
  85. {
  86. self.photoPreViewBottomV.indexPathsForSelectedItems = self.indexPathsForSelectedItems;
  87. }
  88. #pragma mark 刷新导航栏的UI
  89. - (void)refreshNaviBarAndBottomBarState
  90. {
  91. self.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",(_currentIndex+1),_assets.count];
  92. TZAssetModel *model = self.assets[_currentIndex];
  93. self.selectButton.selected = model.isSelected;
  94. }
  95. - (void)setupPhotoPreviewNavBarView
  96. {
  97. [self.backBtn setImage:[UIImage imageNamed:@"icon_white_back"] forState:UIControlStateNormal];
  98. self.navBarBGView.backgroundColor = [UIColor blackColor];
  99. self.titleLabel.textColor = [UIColor whiteColor];
  100. UIButton *but = [[UIButton alloc] init];
  101. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  102. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  103. [but addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  104. [self.navBarBGView addSubview:but];
  105. self.selectButton = but;
  106. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.right.mas_equalTo(-15);
  108. make.width.mas_equalTo(40);
  109. make.height.mas_equalTo(40);
  110. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0.f);
  111. }];
  112. }
  113. - (void)setupPhotoPreviewCollectionView
  114. {
  115. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  116. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  117. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  118. collectionView.dataSource = self;
  119. collectionView.delegate = self;
  120. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  121. collectionView.pagingEnabled = YES;
  122. collectionView.showsVerticalScrollIndicator = NO;
  123. collectionView.showsHorizontalScrollIndicator = NO;
  124. [self.view insertSubview:collectionView atIndex:0];
  125. collectionView.backgroundColor = [UIColor blackColor];
  126. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
  127. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCellGIF"];
  128. [collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
  129. [collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
  130. self.photoPreviewCollectionV = collectionView;
  131. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.left.mas_equalTo(0);
  133. make.right.mas_equalTo(0);
  134. make.bottom.mas_equalTo(-(60 + safeArea));
  135. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  136. }];
  137. }
  138. - (void)setupPhotoPreViewBottomView
  139. {
  140. photoPreViewBottomView * bottomView = [[photoPreViewBottomView alloc] init];
  141. [self.view addSubview:bottomView];
  142. self.photoPreViewBottomV = bottomView;
  143. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.left.mas_equalTo(0);
  145. make.right.mas_equalTo(0);
  146. make.bottom.mas_equalTo(0);
  147. make.height.mas_equalTo((60 + safeArea));
  148. }];
  149. }
  150. #pragma mark - UIScrollViewDelegate
  151. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  152. CGFloat offSetWidth = scrollView.contentOffset.x;
  153. offSetWidth = offSetWidth + (SCREEN_W * 0.5);
  154. NSInteger currentIndex = offSetWidth / (SCREEN_W + 20);
  155. if (currentIndex < _assets.count && _currentIndex != currentIndex && _canSetCurrentIndex) {
  156. _currentIndex = currentIndex;
  157. [self refreshNaviBarAndBottomBarState];
  158. }
  159. [[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
  160. }
  161. #pragma mark - uicollectionDelegate
  162. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  163. return self.assets.count;
  164. }
  165. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  166. TZAssetModel *model = self.assets[indexPath.row];
  167. TZAssetPreviewCell *cell;
  168. __weak typeof(self) weakSelf = self;
  169. if (model.type == TZAssetModelMediaTypeVideo) {
  170. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
  171. TZVideoPreviewCell *currentCell = (TZVideoPreviewCell *)cell;
  172. currentCell.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  173. model.iCloudFailed = isSyncFailed;
  174. //[weakSelf didICloudSyncStatusChanged:model];
  175. };
  176. } else if (model.type == TZAssetModelMediaTypePhotoGif) {
  177. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
  178. TZGifPreviewCell *currentCell = (TZGifPreviewCell *)cell;
  179. currentCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  180. model.iCloudFailed = isSyncFailed;
  181. //[weakSelf didICloudSyncStatusChanged:model];
  182. };
  183. } else {
  184. NSString *reuseId = model.type == TZAssetModelMediaTypePhotoGif ? @"TZPhotoPreviewCellGIF" : @"TZPhotoPreviewCell";
  185. cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
  186. TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
  187. //photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
  188. //photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
  189. //photoPreviewCell.scaleAspectFillCrop = _tzImagePickerVc.scaleAspectFillCrop;
  190. // __weak typeof(_collectionView) weakCollectionView = _collectionView;
  191. // __weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
  192. // [photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
  193. // __strong typeof(weakSelf) strongSelf = weakSelf;
  194. // __strong typeof(weakCollectionView) strongCollectionView = weakCollectionView;
  195. // __strong typeof(weakCell) strongCell = weakCell;
  196. // strongSelf.progress = progress;
  197. // if (progress >= 1) {
  198. // if (strongSelf.isSelectOriginalPhoto) [strongSelf showPhotoBytes];
  199. // if (strongSelf.alertView && [strongCollectionView.visibleCells containsObject:strongCell]) {
  200. // [strongSelf.alertView dismissViewControllerAnimated:YES completion:^{
  201. // strongSelf.alertView = nil;
  202. // [strongSelf doneButtonClick];
  203. // }];
  204. // }
  205. // }
  206. // }];
  207. photoPreviewCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  208. model.iCloudFailed = isSyncFailed;
  209. //[weakSelf didICloudSyncStatusChanged:model];
  210. };
  211. }
  212. cell.model = model;
  213. [cell setSingleTapGestureBlock:^{
  214. __strong typeof(weakSelf) strongSelf = weakSelf;
  215. //[strongSelf didTapPreviewCell];
  216. }];
  217. return cell;
  218. }
  219. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  220. return UIEdgeInsetsMake(0, 0, 0, 0);
  221. }
  222. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  223. CGFloat wh = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - (60 + safeArea);
  224. return CGSizeMake(SCREEN_W, wh);
  225. }
  226. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  227. return 0.0;
  228. }
  229. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  230. return 0.0;
  231. }
  232. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  233. }
  234. @end