PhotoPreviewViewController.m 16 KB

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