123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- //
- // PhotoPreviewViewController.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/11.
- //
- #import "PhotoPreviewViewController.h"
- #import "TZPhotoPreviewCell.h"
- #import "photoPreViewBottomView.h"
- #import "uploadFileRecordViewController.h"
- @interface PhotoPreviewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
- @property (strong, nonatomic) UIButton *selectButton;
- @property (strong, nonatomic) UICollectionView *photoPreviewCollectionV;
- @property (strong, nonatomic) photoPreViewBottomView *photoPreViewBottomV;
- @property (assign, nonatomic) BOOL canSetCurrentIndex;//
- @property (strong, nonatomic)TZVideoPreviewCell * curCell;
- @property (assign, nonatomic) BOOL didChangeSelectIndexType;//是否重新选择过
- @end
- @implementation PhotoPreviewViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self.view setBackgroundColor:[UIColor blackColor]];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPreviewCellDidPlayerFun) name:@"TZ_VIDEO_PLAY_NOTIFICATION" object:nil];
-
-
- [self.toolBar setHidden:YES];
- [self.navigationBar setHidden:YES];
- [self.navBarBGView setHidden:NO];
-
- [self setupPhotoPreviewNavBarView];
- //列表view
- [self setupPhotoPreviewCollectionView];
- [self setupPhotoPreViewBottomView];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear: animated];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
-
- //[self.photoPreviewCollectionV reloadData];
-
- if (_currentIndex >= 0) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self->_canSetCurrentIndex = YES;
- [self.photoPreviewCollectionV setContentOffset:CGPointMake(SCREEN_W * self.currentIndex, 0) animated:NO];
- });
- }
-
- [self refreshNaviBarAndBottomBarState];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- ///黑色
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
-
- [UIApplication sharedApplication].statusBarHidden = NO;
- [self.curCell pausePlayerAndShowNaviBar];
- }
- - (void)backBtnPressed{
- [super backBtnPressed];
-
- if(_changeSelectIndex && _didChangeSelectIndexType){
- _changeSelectIndex(_indexPathsForSelectedItems);
- }
- }
- #pragma mark 点击选中按钮
- - (void)didClickButtonFun:(UIButton*)but
- {
- but.selected = !but.selected;
-
- [self handlCellSelectFun];
-
- _didChangeSelectIndexType = YES;
- }
- #pragma mark 处理点击选中相关
- - (void)handlCellSelectFun
- {
-
- TZAssetModel *model = self.assets[_currentIndex];
-
- //超出最大限制
- // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection ) {
- // return;
- // }
-
- //取消选中
- if ([self.indexPathsForSelectedItems containsObject:model]) {
- [self.indexPathsForSelectedItems removeObject:model];
- model.isSelected = NO;
- }
- else{//选中
- [self.indexPathsForSelectedItems addObject:model];
- model.isSelected = YES;
-
- if(!model.imageData)
- {
- PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
- options.networkAccessAllowed = YES; // Allow downloading from iCloud
- options.version = PHImageRequestOptionsVersionCurrent;
- options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
-
- [[PHImageManager defaultManager] requestImageDataForAsset:model.asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- //model.imageData = imageData;
- model.totalBytes = [imageData length];
- }
- if(model.type != TZAssetModelMediaTypeVideo){
- [self setDataToBottomViewFun];
- }
- }];
-
- if(model.type == TZAssetModelMediaTypeVideo){
-
- PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
- options.version = PHVideoRequestOptionsVersionOriginal;
- options.networkAccessAllowed = YES; // Allow downloading from iCloud
- [[PHImageManager defaultManager] requestAVAssetForVideo:model.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
- if ([asset isKindOfClass:[AVURLAsset class]]) {
-
- AVURLAsset* urlAsset = (AVURLAsset*)asset;
- //NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
- NSNumber *size;
- [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
- //NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0));
-
-
- //model.videoData = videoData;
- //model.totalBytes = [videoData length];
- model.totalBytes = size.longLongValue;
- [self setDataToBottomViewFun];
- }
- }];
- }
- }
- }
-
-
- [self setDataToBottomViewFun];
- //[self refreshNaviBarAndBottomBarState];
- }
- #pragma mark 同步数据到底部
- - (void)setDataToBottomViewFun
- {
- mainBlock(^{
- self.photoPreViewBottomV.indexPathsForSelectedItems = self.indexPathsForSelectedItems;
- });
-
- }
- #pragma mark 刷新导航栏的UI
- - (void)refreshNaviBarAndBottomBarState
- {
- self.titleLabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",(_currentIndex+1),_assets.count];
-
- TZAssetModel *model = self.assets[_currentIndex];
-
- self.selectButton.selected = model.isSelected;
- }
- - (void)setupPhotoPreviewNavBarView
- {
- [self.backBtn setImage:[UIImage imageNamed:@"icon_white_back"] forState:UIControlStateNormal];
- self.navBarBGView.backgroundColor = [UIColor blackColor];
- self.titleLabel.textColor = [UIColor whiteColor];
-
- UIButton *but = [[UIButton alloc] init];
- [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
- [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
- [but addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
- [self.navBarBGView addSubview:but];
- self.selectButton = but;
-
- [but mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(-15);
- make.width.mas_equalTo(40);
- make.height.mas_equalTo(40);
- make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0.f);
-
- }];
- }
- - (void)setupPhotoPreviewCollectionView
- {
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
- [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
-
- UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
- collectionView.dataSource = self;
- collectionView.delegate = self;
- collectionView.translatesAutoresizingMaskIntoConstraints = NO;
- collectionView.pagingEnabled = YES;
- collectionView.showsVerticalScrollIndicator = NO;
- collectionView.showsHorizontalScrollIndicator = NO;
- [self.view insertSubview:collectionView atIndex:0];
- collectionView.backgroundColor = [UIColor blackColor];
-
-
- [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
- [collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCellGIF"];
- [collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
- [collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
-
- self.photoPreviewCollectionV = collectionView;
-
- [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.right.mas_equalTo(0);
- make.bottom.mas_equalTo(-(60 + safeArea));
- make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
- }];
-
- }
- - (void)setupPhotoPreViewBottomView
- {
- photoPreViewBottomView * bottomView = [[photoPreViewBottomView alloc] init];
- [self.view addSubview:bottomView];
- self.photoPreViewBottomV = bottomView;
-
- NSString * uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
-
- BOOL isExtraFileType = NO;
- BOOL isExtraPathFind = NO;
- if(uploadDefaultPath){
- if(![uploadDefaultPath containsString:@"sdcard/"]){
- isExtraFileType = YES;
- }
- }
-
- if(uploadDefaultPath && ksharedAppDelegate.cloudPhoneExtraFileListMod){
- NSArray*arr = ksharedAppDelegate.cloudPhoneExtraFileListMod.data;
-
- for (cloudPhoneExtraFileModel*model in arr) {
- if([uploadDefaultPath containsString:model.extraPath]){
-
- NSString * availableStorage = @"";
- NSInteger totalSize_k = model.extraAvableSize / 1024;
- if(totalSize_k < 1024){
- availableStorage = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
- }
- else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
- availableStorage = [[NSString alloc] initWithFormat:@"%.2fMB",totalSize_k/1024.0];
-
- }
- else{
- availableStorage = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
- }
-
-
- self.photoPreViewBottomV.availableStorage = availableStorage;
- //self.photoPreViewBottomV.available = model.extraAvableSize;
- isExtraPathFind = YES;
- break;
- }
- }
-
-
- }
-
- if(!isExtraFileType){
- self.photoPreViewBottomV.availableStorage = _availableStorage;
- //self.photoPreViewBottomV.available = baseInfoModel.data.available;
- }
-
- if(isExtraFileType && !isExtraPathFind){
- self.photoPreViewBottomV.isExtraFileAndNotFindPathType = YES;
- }
- else{
- self.photoPreViewBottomV.isExtraFileAndNotFindPathType = NO;
- }
-
- //self.photoPreViewBottomV.availableStorage = _availableStorage;
- [self setDataToBottomViewFun];
-
- [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.right.mas_equalTo(0);
- make.bottom.mas_equalTo(0);
- make.height.mas_equalTo((60 + safeArea));
- }];
-
- KWeakSelf
- bottomView.didClickUploadFile = ^{
- [weakSelf gotoUploadFileRecordFun];
- };
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- CGFloat offSetWidth = scrollView.contentOffset.x;
- offSetWidth = offSetWidth + (SCREEN_W * 0.5);
-
- //NSInteger currentIndex = offSetWidth / (SCREEN_W + 20);
- NSInteger currentIndex = offSetWidth / (SCREEN_W);
- if (currentIndex < _assets.count && _currentIndex != currentIndex && _canSetCurrentIndex)
- {
- _currentIndex = currentIndex;
- [self refreshNaviBarAndBottomBarState];
- }
- [[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
- }
- #pragma mark - uicollectionDelegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.assets.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- TZAssetModel *model = self.assets[indexPath.row];
-
- TZAssetPreviewCell *cell;
- __weak typeof(self) weakSelf = self;
- if (model.type == TZAssetModelMediaTypeVideo) {
- cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
- TZVideoPreviewCell *currentCell = (TZVideoPreviewCell *)cell;
- currentCell.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
- model.iCloudFailed = isSyncFailed;
- //[weakSelf didICloudSyncStatusChanged:model];
- };
- } else if (model.type == TZAssetModelMediaTypePhotoGif) {
- cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
- TZGifPreviewCell *currentCell = (TZGifPreviewCell *)cell;
- currentCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
- model.iCloudFailed = isSyncFailed;
- //[weakSelf didICloudSyncStatusChanged:model];
- };
- } else {
- NSString *reuseId = model.type == TZAssetModelMediaTypePhotoGif ? @"TZPhotoPreviewCellGIF" : @"TZPhotoPreviewCell";
- cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
- TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
- //photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
- //photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
- //photoPreviewCell.scaleAspectFillCrop = _tzImagePickerVc.scaleAspectFillCrop;
- // __weak typeof(_collectionView) weakCollectionView = _collectionView;
- // __weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
- // [photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
- // __strong typeof(weakSelf) strongSelf = weakSelf;
- // __strong typeof(weakCollectionView) strongCollectionView = weakCollectionView;
- // __strong typeof(weakCell) strongCell = weakCell;
- // strongSelf.progress = progress;
- // if (progress >= 1) {
- // if (strongSelf.isSelectOriginalPhoto) [strongSelf showPhotoBytes];
- // if (strongSelf.alertView && [strongCollectionView.visibleCells containsObject:strongCell]) {
- // [strongSelf.alertView dismissViewControllerAnimated:YES completion:^{
- // strongSelf.alertView = nil;
- // [strongSelf doneButtonClick];
- // }];
- // }
- // }
- // }];
- photoPreviewCell.previewView.iCloudSyncFailedHandle = ^(id asset, BOOL isSyncFailed) {
- model.iCloudFailed = isSyncFailed;
- //[weakSelf didICloudSyncStatusChanged:model];
- };
- }
-
- cell.model = model;
- // [cell setSingleTapGestureBlock:^{
- // __strong typeof(weakSelf) strongSelf = weakSelf;
- // //[strongSelf didTapPreviewCell];
- //
- // if([cell isKindOfClass:[TZVideoPreviewCell class]]){
- // weakSelf.curCell = cell;
- // }
- //
- // }];
- return cell;
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(0, 0, 0, 0);
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat wh = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - (60 + safeArea);
- return CGSizeMake(SCREEN_W, wh);
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 0.0;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 0.0;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- }
- #pragma mark 跳转上传记录
- - (void)gotoUploadFileRecordFun
- {
- [UIApplication sharedApplication].statusBarHidden = NO;
-
- uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
- [self.navigationController pushViewController:vc animated:YES];
- vc.isUploadingType = YES;
- [vc gotoUploadFile:_indexPathsForSelectedItems];
-
- if(_isPhotoType){
- //数据埋点
- [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Image_upload"];
- }
- else{
- //数据埋点
- [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Video_upload"];
- }
- }
- #pragma mark TZVideoPreviewCell视频播放
- - (void)videoPreviewCellDidPlayerFun
- {
- //[UIApplication sharedApplication].statusBarHidden = NO;
- }
- @end
|