123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- //
- // PhotoPreviewViewController.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/11.
- //
- #import "PhotoPreviewViewController.h"
- #import "TZPhotoPreviewCell.h"
- #import "photoPreViewBottomView.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;//
- @end
- @implementation PhotoPreviewViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self.view setBackgroundColor:[UIColor blackColor]];
-
- [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) {
- 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];
- }
- #pragma mark 点击选中按钮
- - (void)didClickButtonFun:(UIButton*)but
- {
- but.selected = !but.selected;
- }
- #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 = NO;
-
- if(!model.imageData)
- {
- [[PHImageManager defaultManager] requestImageDataForAsset:model.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- model.imageData = imageData;
- }
-
- [self setDataToBottomViewFun];
- }];
- }
- }
-
-
- [self setDataToBottomViewFun];
- [self refreshNaviBarAndBottomBarState];
- }
- #pragma mark 同步数据到底部
- - (void)setDataToBottomViewFun
- {
- 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;
-
- [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));
- }];
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- CGFloat offSetWidth = scrollView.contentOffset.x;
- offSetWidth = offSetWidth + (SCREEN_W * 0.5);
-
- NSInteger currentIndex = offSetWidth / (SCREEN_W + 20);
- 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];
- }];
- 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 {
-
-
- }
- @end
|