PhotoPreviewViewController.m 14 KB

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