PhotoPreviewViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. model.totalBytes = [imageData length];
  88. }
  89. if(model.type != TZAssetModelMediaTypeVideo){
  90. [self setDataToBottomViewFun];
  91. }
  92. }];
  93. if(model.type == TZAssetModelMediaTypeVideo){
  94. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  95. options.version = PHVideoRequestOptionsVersionOriginal;
  96. [[PHImageManager defaultManager] requestAVAssetForVideo:model.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  97. if ([asset isKindOfClass:[AVURLAsset class]]) {
  98. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  99. NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  100. // NSNumber *size;
  101. // [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
  102. // NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0));
  103. //model.videoData = videoData;
  104. model.totalBytes = [videoData length];
  105. [self setDataToBottomViewFun];
  106. }
  107. }];
  108. }
  109. }
  110. }
  111. [self setDataToBottomViewFun];
  112. //[self refreshNaviBarAndBottomBarState];
  113. }
  114. #pragma mark 同步数据到底部
  115. - (void)setDataToBottomViewFun
  116. {
  117. mainBlock(^{
  118. self.photoPreViewBottomV.indexPathsForSelectedItems = self.indexPathsForSelectedItems;
  119. });
  120. }
  121. #pragma mark 刷新导航栏的UI
  122. - (void)refreshNaviBarAndBottomBarState
  123. {
  124. self.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",(_currentIndex+1),_assets.count];
  125. TZAssetModel *model = self.assets[_currentIndex];
  126. self.selectButton.selected = model.isSelected;
  127. }
  128. - (void)setupPhotoPreviewNavBarView
  129. {
  130. [self.backBtn setImage:[UIImage imageNamed:@"icon_white_back"] forState:UIControlStateNormal];
  131. self.navBarBGView.backgroundColor = [UIColor blackColor];
  132. self.titleLabel.textColor = [UIColor whiteColor];
  133. UIButton *but = [[UIButton alloc] init];
  134. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  135. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  136. [but addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  137. [self.navBarBGView addSubview:but];
  138. self.selectButton = but;
  139. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.right.mas_equalTo(-15);
  141. make.width.mas_equalTo(40);
  142. make.height.mas_equalTo(40);
  143. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0.f);
  144. }];
  145. }
  146. - (void)setupPhotoPreviewCollectionView
  147. {
  148. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  149. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  150. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  151. collectionView.dataSource = self;
  152. collectionView.delegate = self;
  153. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  154. collectionView.pagingEnabled = YES;
  155. collectionView.showsVerticalScrollIndicator = NO;
  156. collectionView.showsHorizontalScrollIndicator = NO;
  157. [self.view insertSubview:collectionView atIndex:0];
  158. collectionView.backgroundColor = [UIColor blackColor];
  159. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
  160. [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCellGIF"];
  161. [collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
  162. [collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
  163. self.photoPreviewCollectionV = collectionView;
  164. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  165. make.left.mas_equalTo(0);
  166. make.right.mas_equalTo(0);
  167. make.bottom.mas_equalTo(-(60 + safeArea));
  168. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  169. }];
  170. }
  171. - (void)setupPhotoPreViewBottomView
  172. {
  173. photoPreViewBottomView * bottomView = [[photoPreViewBottomView alloc] init];
  174. [self.view addSubview:bottomView];
  175. self.photoPreViewBottomV = bottomView;
  176. NSString * uploadDefaultPath = [HWDataManager getStringWithKey:Const_photo_upload_default_path];
  177. BOOL isExtraFileType = NO;
  178. BOOL isExtraPathFind = NO;
  179. if(uploadDefaultPath){
  180. if(![uploadDefaultPath containsString:@"sdcard/"]){
  181. isExtraFileType = YES;
  182. }
  183. }
  184. if(uploadDefaultPath && ksharedAppDelegate.cloudPhoneExtraFileListMod){
  185. NSArray*arr = ksharedAppDelegate.cloudPhoneExtraFileListMod.data;
  186. for (cloudPhoneExtraFileModel*model in arr) {
  187. if([uploadDefaultPath containsString:model.extraPath]){
  188. NSString * availableStorage = @"";
  189. NSInteger totalSize_k = model.extraAvableSize / 1024;
  190. if(totalSize_k < 1024){
  191. availableStorage = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  192. }
  193. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  194. availableStorage = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  195. }
  196. else{
  197. availableStorage = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  198. }
  199. self.photoPreViewBottomV.availableStorage = availableStorage;
  200. //self.photoPreViewBottomV.available = model.extraAvableSize;
  201. isExtraPathFind = YES;
  202. break;
  203. }
  204. }
  205. }
  206. if(!isExtraFileType){
  207. self.photoPreViewBottomV.availableStorage = _availableStorage;
  208. //self.photoPreViewBottomV.available = baseInfoModel.data.available;
  209. }
  210. if(isExtraFileType && !isExtraPathFind){
  211. self.photoPreViewBottomV.isExtraFileAndNotFindPathType = YES;
  212. }
  213. else{
  214. self.photoPreViewBottomV.isExtraFileAndNotFindPathType = NO;
  215. }
  216. //self.photoPreViewBottomV.availableStorage = _availableStorage;
  217. [self setDataToBottomViewFun];
  218. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  219. make.left.mas_equalTo(0);
  220. make.right.mas_equalTo(0);
  221. make.bottom.mas_equalTo(0);
  222. make.height.mas_equalTo((60 + safeArea));
  223. }];
  224. KWeakSelf
  225. bottomView.didClickUploadFile = ^{
  226. [weakSelf gotoUploadFileRecordFun];
  227. };
  228. }
  229. #pragma mark - UIScrollViewDelegate
  230. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  231. CGFloat offSetWidth = scrollView.contentOffset.x;
  232. offSetWidth = offSetWidth + (SCREEN_W * 0.5);
  233. NSInteger currentIndex = offSetWidth / (SCREEN_W + 20);
  234. if (currentIndex < _assets.count && _currentIndex != currentIndex && _canSetCurrentIndex)
  235. {
  236. _currentIndex = currentIndex;
  237. [self refreshNaviBarAndBottomBarState];
  238. }
  239. [[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
  240. }
  241. #pragma mark - uicollectionDelegate
  242. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  243. return self.assets.count;
  244. }
  245. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  246. TZAssetModel *model = self.assets[indexPath.row];
  247. TZAssetPreviewCell *cell;
  248. __weak typeof(self) weakSelf = self;
  249. if (model.type == TZAssetModelMediaTypeVideo) {
  250. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
  251. TZVideoPreviewCell *currentCell = (TZVideoPreviewCell *)cell;
  252. currentCell.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  253. model.iCloudFailed = isSyncFailed;
  254. //[weakSelf didICloudSyncStatusChanged:model];
  255. };
  256. } else if (model.type == TZAssetModelMediaTypePhotoGif) {
  257. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
  258. TZGifPreviewCell *currentCell = (TZGifPreviewCell *)cell;
  259. currentCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  260. model.iCloudFailed = isSyncFailed;
  261. //[weakSelf didICloudSyncStatusChanged:model];
  262. };
  263. } else {
  264. NSString *reuseId = model.type == TZAssetModelMediaTypePhotoGif ? @"TZPhotoPreviewCellGIF" : @"TZPhotoPreviewCell";
  265. cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
  266. TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
  267. //photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
  268. //photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
  269. //photoPreviewCell.scaleAspectFillCrop = _tzImagePickerVc.scaleAspectFillCrop;
  270. // __weak typeof(_collectionView) weakCollectionView = _collectionView;
  271. // __weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
  272. // [photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
  273. // __strong typeof(weakSelf) strongSelf = weakSelf;
  274. // __strong typeof(weakCollectionView) strongCollectionView = weakCollectionView;
  275. // __strong typeof(weakCell) strongCell = weakCell;
  276. // strongSelf.progress = progress;
  277. // if (progress >= 1) {
  278. // if (strongSelf.isSelectOriginalPhoto) [strongSelf showPhotoBytes];
  279. // if (strongSelf.alertView && [strongCollectionView.visibleCells containsObject:strongCell]) {
  280. // [strongSelf.alertView dismissViewControllerAnimated:YES completion:^{
  281. // strongSelf.alertView = nil;
  282. // [strongSelf doneButtonClick];
  283. // }];
  284. // }
  285. // }
  286. // }];
  287. photoPreviewCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
  288. model.iCloudFailed = isSyncFailed;
  289. //[weakSelf didICloudSyncStatusChanged:model];
  290. };
  291. }
  292. cell.model = model;
  293. // [cell setSingleTapGestureBlock:^{
  294. // __strong typeof(weakSelf) strongSelf = weakSelf;
  295. // //[strongSelf didTapPreviewCell];
  296. //
  297. // if([cell isKindOfClass:[TZVideoPreviewCell class]]){
  298. // weakSelf.curCell = cell;
  299. // }
  300. //
  301. // }];
  302. return cell;
  303. }
  304. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  305. return UIEdgeInsetsMake(0, 0, 0, 0);
  306. }
  307. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  308. CGFloat wh = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - (60 + safeArea);
  309. return CGSizeMake(SCREEN_W, wh);
  310. }
  311. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  312. return 0.0;
  313. }
  314. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  315. return 0.0;
  316. }
  317. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  318. }
  319. #pragma mark 跳转上传记录
  320. - (void)gotoUploadFileRecordFun
  321. {
  322. [UIApplication sharedApplication].statusBarHidden = NO;
  323. uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
  324. [self.navigationController pushViewController:vc animated:YES];
  325. vc.isUploadingType = YES;
  326. [vc gotoUploadFile:_indexPathsForSelectedItems];
  327. }
  328. #pragma mark TZVideoPreviewCell视频播放
  329. - (void)videoPreviewCellDidPlayerFun
  330. {
  331. //[UIApplication sharedApplication].statusBarHidden = NO;
  332. }
  333. @end