PhotoPreviewViewController.m 14 KB

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