123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- //
- // uploadImageOrVideoViewController.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/9.
- //
- #import "uploadImageOrVideoViewController.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import <AVFoundation/AVFoundation.h>
- #import "AJPhotoListView.h"
- #import "AJPhotoGroupView.h"
- #import "AJPhotoListCell.h"
- #import "uploadFileBottomView.h"
- #import "PhotoPreviewViewController.h"
- @interface uploadImageOrVideoViewController ()<AJPhotoGroupViewProtocol,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
- @property (strong, nonatomic) AJPhotoGroupView *photoGroupView;
- @property (strong, nonatomic) UILabel *MytitleLabel;
- @property (strong, nonatomic) UIImageView *selectTip;
- @property (nonatomic) BOOL isNotAllowed;
- @property (strong, nonatomic) UIView *bgMaskView;
- @property (strong, nonatomic) AJPhotoListView *photoListView;
- @property (strong, nonatomic) NSMutableArray *assets;
- @property (strong, nonatomic) NSIndexPath *lastAccessed;
- @property (strong, nonatomic) uploadFileBottomView *uploadFileBottomV;
- //最多选择项
- @property (nonatomic, assign) NSInteger maximumNumberOfSelection;
- //最少选择项
- @property (nonatomic, assign) NSInteger minimumNumberOfSelection;
- //是否开启多选
- @property (nonatomic, assign) BOOL multipleSelection;
- @end
- @implementation uploadImageOrVideoViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self.view setBackgroundColor:HWF5F7FAColor];
-
- [self.toolBar setHidden:YES];
- [self.navigationBar setHidden:YES];
- [self.navBarBGView setHidden:NO];
-
- [self initBaselUIFun];
-
- _isNotAllowed = YES;
- _maximumNumberOfSelection = 3;
- _minimumNumberOfSelection = 0;
- _multipleSelection = YES;
-
- if(!_selectionFilter)
- {
- _selectionFilter = [NSPredicate predicateWithValue:YES];
- }
- [self checkVideoAccessFun];
-
- }
- - (void)initBaselUIFun
- {
- [self initNavHeadUIFun];
-
- //列表view
- [self setupPhotoListView];
- [self setupUploadFileBottomView];
- //相册分组
- [self setupGroupView];
- }
- - (void)initNavHeadUIFun
- {
- //title
- UILabel *titleLabel = [[UILabel alloc] init];
- titleLabel.textAlignment = NSTextAlignmentCenter;
- titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
- titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
- [self.navBarBGView addSubview:titleLabel];
- self.MytitleLabel = titleLabel;
-
- //selectTipImageView
- UIImageView *selectTip = [[UIImageView alloc] init];
- selectTip.image = [UIImage imageNamed:@"upload_image_arrow"];
- selectTip.translatesAutoresizingMaskIntoConstraints = NO;
- [self.navBarBGView addSubview:selectTip];
- self.selectTip = selectTip;
-
- UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- tapBtn.backgroundColor = [UIColor clearColor];
- tapBtn.translatesAutoresizingMaskIntoConstraints = NO;
- [tapBtn addTarget:self action:@selector(selectGroupAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.navBarBGView addSubview:tapBtn];
-
- [tapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.mas_equalTo(self.navBarBGView.mas_centerX);
- make.centerY.mas_equalTo(self.backBtn.mas_centerY);
- make.width.mas_equalTo(100);
- make.height.mas_equalTo(30);
- }];
-
- [self setTitleLabelText:@"所有相片"];
- }
- #pragma mark 设置相册标题
- - (void)setTitleLabelText:(NSString*)title
- {
- CGFloat curWidth = [title boundingRectWithSize:CGSizeMake(SCREEN_W, 30) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.f]} context:nil].size.width;
-
- self.MytitleLabel.text = title;
- curWidth += 20;
- //HLog(@"title w:%f",curWidth);
- [self.MytitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.centerX.mas_equalTo(self.navBarBGView.mas_centerX).offset(-10);
- make.centerY.mas_equalTo(self.backBtn.mas_centerY);
- make.width.mas_equalTo(curWidth);
- make.height.mas_equalTo(30);
- }];
- //self.MytitleLabel.backgroundColor = [UIColor greenColor];
-
- [self.selectTip mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.MytitleLabel.mas_right).offset(2);
- make.centerY.mas_equalTo(self.backBtn.mas_centerY);
- make.width.mas_equalTo(15);
- make.height.mas_equalTo(15);
- }];
- }
- #pragma mark 判断手机相册权限
- - (void)checkVideoAccessFun
- {
- AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- //判断摄像头状态是否可用
- if(authStatus==AVAuthorizationStatusAuthorized){
- _isNotAllowed = NO;
- }else{
- NSLog(@"未开启相机权限,请前往设置中开启");
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
- if (granted){
- self->_isNotAllowed = NO;
- // mainBlock(^{
- //
- // });
- }
- }];
- }
- }
- /**
- * 照片列表
- */
- - (void)setupPhotoListView {
- AJPhotoListView *collectionView = [[AJPhotoListView alloc] init];
- collectionView.dataSource = self;
- collectionView.delegate = self;
- collectionView.translatesAutoresizingMaskIntoConstraints = NO;
- [self.view insertSubview:collectionView atIndex:0];
- self.photoListView = 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)setupUploadFileBottomView
- {
- uploadFileBottomView *bottomView = [[uploadFileBottomView alloc] init];
- [self.view insertSubview:bottomView atIndex:0];
- self.uploadFileBottomV = 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);
- }];
- }
- /**
- * 相册
- */
- - (void)setupGroupView {
- AJPhotoGroupView *photoGroupView = [[AJPhotoGroupView alloc] init];
- photoGroupView.my_delegate = self;
- [self.view insertSubview:photoGroupView belowSubview:self.navBarBGView];
- self.photoGroupView = photoGroupView;
- photoGroupView.hidden = YES;
- photoGroupView.translatesAutoresizingMaskIntoConstraints = NO;
-
- [self.photoGroupView setupGroup];
-
- NSInteger maxCount = 6;
-
- if(self.photoGroupView.albumGroups.count < maxCount){
- maxCount = self.photoGroupView.albumGroups.count;
- }
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [photoGroupView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.right.mas_equalTo(0);
- make.height.mas_equalTo(60.0 * maxCount);
- make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
- }];
- });
-
- }
- #pragma mark - 相册切换
- - (void)selectGroupAction:(UIButton *)sender {
-
- //无权限
- if (self.isNotAllowed) {
- return;
- }
- if (self.photoGroupView.hidden) {
- [self bgMaskView];
- self.bgMaskView.hidden = NO;
- self.photoGroupView.hidden = NO;
- [UIView animateWithDuration:0.3 animations:^{
- CGRect rect = self.photoGroupView.frame;
- rect.origin.y = CGRectGetMaxY(self.navBarBGView.frame);
- self.photoGroupView.frame = rect;
-
- self.selectTip.transform = CGAffineTransformMakeRotation(M_PI);
- }];
- } else {
- [self hidenGroupView];
- }
- }
- - (void)hidenGroupView {
- //[self.bgMaskView removeFromSuperview];
- self.bgMaskView.hidden = YES;
- [UIView animateWithDuration:0.3 animations:^{
- CGRect rect = self.photoGroupView.frame;
- rect.origin.y = -SCREEN_H;
- self.photoGroupView.frame = rect;
-
- self.selectTip.transform = CGAffineTransformIdentity;
- }completion:^(BOOL finished) {
- self.photoGroupView.hidden = YES;
- }];
- }
- #pragma mark - 遮罩背景
- - (UIView *)bgMaskView {
- if (_bgMaskView == nil) {
- UIView *bgMaskView = [[UIView alloc] init];
- bgMaskView.alpha = 0.5;
- bgMaskView.translatesAutoresizingMaskIntoConstraints = NO;
- bgMaskView.backgroundColor = [UIColor blackColor];
- //[self.view insertSubview:bgMaskView aboveSubview:self.photoListView];
- [self.view insertSubview:bgMaskView belowSubview:self.photoGroupView];
-
- bgMaskView.userInteractionEnabled = YES;
- [bgMaskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgMaskView:)]];
- _bgMaskView = bgMaskView;
-
- [bgMaskView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.right.mas_equalTo(0);
- make.bottom.mas_equalTo(0);
- make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
- }];
- }
-
- return _bgMaskView;
- }
- - (void)tapBgMaskView:(UITapGestureRecognizer *)sender {
- if (!self.photoGroupView.hidden) {
- [self hidenGroupView];
- }
- }
- #pragma mark - BoPhotoGroupViewProtocol
- - (void)didSelectGroup:(TZAlbumModel *)model {
- [self loadAssets:model];
- [self setTitleLabelText:model.name];
- [self hidenGroupView];
- }
- //加载图片
- - (void)loadAssets:(TZAlbumModel *)model {
- [self.indexPathsForSelectedItems removeAllObjects];
- [self.assets removeAllObjects];
-
- [[TZImageManager manager] getAssetsFromFetchResult:model.result completion:^(NSArray<TZAssetModel *> *models) {
- self->_assets = [NSMutableArray arrayWithArray:models];
- [self.photoListView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
- [self.photoListView reloadData];
- }];
-
- }
- #pragma mark - uicollectionDelegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.assets.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *cellIdentifer = @"cell";
- AJPhotoListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
-
- BOOL isSelected = [self.indexPathsForSelectedItems containsObject:self.assets[indexPath.row]];
- [cell bind:self.assets[indexPath.row] selectionFilter:self.selectionFilter isSelected:isSelected];
-
- KWeakSelf
- cell.didClckSelectBut = ^(BOOL isSelect) {
- [weakSelf handlCellSelectFunBy:indexPath];
- };
-
- return cell;
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(5, 5, 5, 5);
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat wh = (collectionView.bounds.size.width - 20)/3.0;
- return CGSizeMake(wh, wh);
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 5.0;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 5.0;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- // AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
- // ALAsset *asset = self.assets[indexPath.row];
-
- PhotoPreviewViewController *vc =[PhotoPreviewViewController new];
- vc.assets = _assets;
- vc.currentIndex = indexPath.row;
- [self.navigationController pushViewController:vc animated:YES];
-
- //相机按钮处理
- // if ([asset isKindOfClass:[UIImage class]] && _delegate && [_delegate respondsToSelector:@selector(photoPickerTapCameraAction:)]) {
- // [_delegate photoPickerTapCameraAction:self];
- // return;
- // }
-
- //单选
- // if (!self.multipleSelection && self.indexPathsForSelectedItems.count==1) {
- // //取消上一个选中
- // NSInteger index = [self.assets indexOfObject:self.indexPathsForSelectedItems[0]];
- // NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
- // AJPhotoListCell *previousCell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
- // [previousCell isSelected:NO];
- // [self.indexPathsForSelectedItems removeAllObjects];
- //
- // //选中当前的
- // [self.indexPathsForSelectedItems addObject:asset];
- // [cell isSelected:YES];
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
- // [_delegate photoPicker:self didDeselectAsset:asset];
- // return;
- // }
-
- //超出最大限制
- // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
- // [_delegate photoPickerDidMaximum:self];
- // return;
- // }
- //
- // //选择过滤
- // BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
- // if (!selectable) {
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
- // [_delegate photoPickerDidSelectionFilter:self];
- // return;
- // }
- //
- // //取消选中
- // if ([self.indexPathsForSelectedItems containsObject:asset]) {
- // [self.indexPathsForSelectedItems removeObject:asset];
- // [cell isSelected:NO];
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
- // [_delegate photoPicker:self didDeselectAsset:asset];
- // return;
- // }
- //
- // //选中
- // [self.indexPathsForSelectedItems addObject:asset];
- // [cell isSelected:YES];
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
- // [_delegate photoPicker:self didSelectAsset:asset];
- }
- #pragma mark 处理点击选中相关
- - (void)handlCellSelectFunBy:(NSIndexPath *)indexPath
- {
- // AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
- // ALAsset *asset = self.assets[indexPath.row];
- //
- // //超出最大限制
- // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
- // [_delegate photoPickerDidMaximum:self];
- // return;
- // }
- //
- // //选择过滤
- // BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
- // if (!selectable) {
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
- // [_delegate photoPickerDidSelectionFilter:self];
- // return;
- // }
- //
- // //取消选中
- // if ([self.indexPathsForSelectedItems containsObject:asset]) {
- // [self.indexPathsForSelectedItems removeObject:asset];
- // [cell isSelected:NO];
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
- // [_delegate photoPicker:self didDeselectAsset:asset];
- // return;
- // }
- //
- // //选中
- // [self.indexPathsForSelectedItems addObject:asset];
- // [cell isSelected:YES];
- // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
- // [_delegate photoPicker:self didSelectAsset:asset];
- }
- #pragma mark - getter/setter
- - (NSMutableArray *)assets {
- if (!_assets) {
- _assets = [[NSMutableArray alloc] init];
- }
- return _assets;
- }
- - (NSMutableArray *)indexPathsForSelectedItems {
- if (!_indexPathsForSelectedItems) {
- _indexPathsForSelectedItems = [[NSMutableArray alloc] init];
- }
- return _indexPathsForSelectedItems;
- }
- @end
|