uploadImageOrVideoViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // uploadImageOrVideoViewController.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/9.
  6. //
  7. #import "uploadImageOrVideoViewController.h"
  8. #import <AssetsLibrary/AssetsLibrary.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import "AJPhotoListView.h"
  11. #import "AJPhotoGroupView.h"
  12. #import "AJPhotoListCell.h"
  13. #import "uploadFileBottomView.h"
  14. #import "PhotoPreviewViewController.h"
  15. @interface uploadImageOrVideoViewController ()<AJPhotoGroupViewProtocol,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  16. @property (strong, nonatomic) AJPhotoGroupView *photoGroupView;
  17. @property (strong, nonatomic) UILabel *MytitleLabel;
  18. @property (strong, nonatomic) UIImageView *selectTip;
  19. @property (nonatomic) BOOL isNotAllowed;
  20. @property (strong, nonatomic) UIView *bgMaskView;
  21. @property (strong, nonatomic) AJPhotoListView *photoListView;
  22. @property (strong, nonatomic) NSMutableArray *assets;
  23. @property (strong, nonatomic) NSIndexPath *lastAccessed;
  24. @property (strong, nonatomic) uploadFileBottomView *uploadFileBottomV;
  25. //最多选择项
  26. @property (nonatomic, assign) NSInteger maximumNumberOfSelection;
  27. //最少选择项
  28. @property (nonatomic, assign) NSInteger minimumNumberOfSelection;
  29. //是否开启多选
  30. @property (nonatomic, assign) BOOL multipleSelection;
  31. @end
  32. @implementation uploadImageOrVideoViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. [self.view setBackgroundColor:HWF5F7FAColor];
  37. [self.toolBar setHidden:YES];
  38. [self.navigationBar setHidden:YES];
  39. [self.navBarBGView setHidden:NO];
  40. [self initBaselUIFun];
  41. _isNotAllowed = YES;
  42. _maximumNumberOfSelection = 3;
  43. _minimumNumberOfSelection = 0;
  44. _multipleSelection = YES;
  45. if(!_selectionFilter)
  46. {
  47. _selectionFilter = [NSPredicate predicateWithValue:YES];
  48. }
  49. [self checkVideoAccessFun];
  50. }
  51. - (void)initBaselUIFun
  52. {
  53. [self initNavHeadUIFun];
  54. //列表view
  55. [self setupPhotoListView];
  56. [self setupUploadFileBottomView];
  57. //相册分组
  58. [self setupGroupView];
  59. }
  60. - (void)initNavHeadUIFun
  61. {
  62. //title
  63. UILabel *titleLabel = [[UILabel alloc] init];
  64. titleLabel.textAlignment = NSTextAlignmentCenter;
  65. titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
  66. titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  67. [self.navBarBGView addSubview:titleLabel];
  68. self.MytitleLabel = titleLabel;
  69. //selectTipImageView
  70. UIImageView *selectTip = [[UIImageView alloc] init];
  71. selectTip.image = [UIImage imageNamed:@"upload_image_arrow"];
  72. selectTip.translatesAutoresizingMaskIntoConstraints = NO;
  73. [self.navBarBGView addSubview:selectTip];
  74. self.selectTip = selectTip;
  75. UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  76. tapBtn.backgroundColor = [UIColor clearColor];
  77. tapBtn.translatesAutoresizingMaskIntoConstraints = NO;
  78. [tapBtn addTarget:self action:@selector(selectGroupAction:) forControlEvents:UIControlEventTouchUpInside];
  79. [self.navBarBGView addSubview:tapBtn];
  80. [tapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.centerX.mas_equalTo(self.navBarBGView.mas_centerX);
  82. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  83. make.width.mas_equalTo(100);
  84. make.height.mas_equalTo(30);
  85. }];
  86. [self setTitleLabelText:@"所有相片"];
  87. }
  88. #pragma mark 设置相册标题
  89. - (void)setTitleLabelText:(NSString*)title
  90. {
  91. CGFloat curWidth = [title boundingRectWithSize:CGSizeMake(SCREEN_W, 30) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.f]} context:nil].size.width;
  92. self.MytitleLabel.text = title;
  93. curWidth += 20;
  94. //HLog(@"title w:%f",curWidth);
  95. [self.MytitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  96. make.centerX.mas_equalTo(self.navBarBGView.mas_centerX).offset(-10);
  97. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  98. make.width.mas_equalTo(curWidth);
  99. make.height.mas_equalTo(30);
  100. }];
  101. //self.MytitleLabel.backgroundColor = [UIColor greenColor];
  102. [self.selectTip mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.mas_equalTo(self.MytitleLabel.mas_right).offset(2);
  104. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  105. make.width.mas_equalTo(15);
  106. make.height.mas_equalTo(15);
  107. }];
  108. }
  109. #pragma mark 判断手机相册权限
  110. - (void)checkVideoAccessFun
  111. {
  112. AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  113. //判断摄像头状态是否可用
  114. if(authStatus==AVAuthorizationStatusAuthorized){
  115. _isNotAllowed = NO;
  116. }else{
  117. NSLog(@"未开启相机权限,请前往设置中开启");
  118. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  119. if (granted){
  120. self->_isNotAllowed = NO;
  121. // mainBlock(^{
  122. //
  123. // });
  124. }
  125. }];
  126. }
  127. }
  128. /**
  129. * 照片列表
  130. */
  131. - (void)setupPhotoListView {
  132. AJPhotoListView *collectionView = [[AJPhotoListView alloc] init];
  133. collectionView.dataSource = self;
  134. collectionView.delegate = self;
  135. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  136. [self.view insertSubview:collectionView atIndex:0];
  137. self.photoListView = collectionView;
  138. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.left.mas_equalTo(0);
  140. make.right.mas_equalTo(0);
  141. make.bottom.mas_equalTo(-(60 + safeArea));
  142. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  143. }];
  144. }
  145. /**
  146. * 照片列表
  147. */
  148. - (void)setupUploadFileBottomView
  149. {
  150. uploadFileBottomView *bottomView = [[uploadFileBottomView alloc] init];
  151. [self.view insertSubview:bottomView atIndex:0];
  152. self.uploadFileBottomV = bottomView;
  153. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  154. make.left.mas_equalTo(0);
  155. make.right.mas_equalTo(0);
  156. make.bottom.mas_equalTo(0);
  157. make.height.mas_equalTo(60 + safeArea);
  158. }];
  159. }
  160. /**
  161. * 相册
  162. */
  163. - (void)setupGroupView {
  164. AJPhotoGroupView *photoGroupView = [[AJPhotoGroupView alloc] init];
  165. photoGroupView.my_delegate = self;
  166. [self.view insertSubview:photoGroupView belowSubview:self.navBarBGView];
  167. self.photoGroupView = photoGroupView;
  168. photoGroupView.hidden = YES;
  169. photoGroupView.translatesAutoresizingMaskIntoConstraints = NO;
  170. [self.photoGroupView setupGroup];
  171. NSInteger maxCount = 6;
  172. if(self.photoGroupView.albumGroups.count < maxCount){
  173. maxCount = self.photoGroupView.albumGroups.count;
  174. }
  175. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  176. [photoGroupView mas_makeConstraints:^(MASConstraintMaker *make) {
  177. make.left.mas_equalTo(0);
  178. make.right.mas_equalTo(0);
  179. make.height.mas_equalTo(60.0 * maxCount);
  180. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  181. }];
  182. });
  183. }
  184. #pragma mark - 相册切换
  185. - (void)selectGroupAction:(UIButton *)sender {
  186. //无权限
  187. if (self.isNotAllowed) {
  188. return;
  189. }
  190. if (self.photoGroupView.hidden) {
  191. [self bgMaskView];
  192. self.bgMaskView.hidden = NO;
  193. self.photoGroupView.hidden = NO;
  194. [UIView animateWithDuration:0.3 animations:^{
  195. CGRect rect = self.photoGroupView.frame;
  196. rect.origin.y = CGRectGetMaxY(self.navBarBGView.frame);
  197. self.photoGroupView.frame = rect;
  198. self.selectTip.transform = CGAffineTransformMakeRotation(M_PI);
  199. }];
  200. } else {
  201. [self hidenGroupView];
  202. }
  203. }
  204. - (void)hidenGroupView {
  205. //[self.bgMaskView removeFromSuperview];
  206. self.bgMaskView.hidden = YES;
  207. [UIView animateWithDuration:0.3 animations:^{
  208. CGRect rect = self.photoGroupView.frame;
  209. rect.origin.y = -SCREEN_H;
  210. self.photoGroupView.frame = rect;
  211. self.selectTip.transform = CGAffineTransformIdentity;
  212. }completion:^(BOOL finished) {
  213. self.photoGroupView.hidden = YES;
  214. }];
  215. }
  216. #pragma mark - 遮罩背景
  217. - (UIView *)bgMaskView {
  218. if (_bgMaskView == nil) {
  219. UIView *bgMaskView = [[UIView alloc] init];
  220. bgMaskView.alpha = 0.5;
  221. bgMaskView.translatesAutoresizingMaskIntoConstraints = NO;
  222. bgMaskView.backgroundColor = [UIColor blackColor];
  223. //[self.view insertSubview:bgMaskView aboveSubview:self.photoListView];
  224. [self.view insertSubview:bgMaskView belowSubview:self.photoGroupView];
  225. bgMaskView.userInteractionEnabled = YES;
  226. [bgMaskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgMaskView:)]];
  227. _bgMaskView = bgMaskView;
  228. [bgMaskView mas_makeConstraints:^(MASConstraintMaker *make) {
  229. make.left.mas_equalTo(0);
  230. make.right.mas_equalTo(0);
  231. make.bottom.mas_equalTo(0);
  232. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  233. }];
  234. }
  235. return _bgMaskView;
  236. }
  237. - (void)tapBgMaskView:(UITapGestureRecognizer *)sender {
  238. if (!self.photoGroupView.hidden) {
  239. [self hidenGroupView];
  240. }
  241. }
  242. #pragma mark - BoPhotoGroupViewProtocol
  243. - (void)didSelectGroup:(TZAlbumModel *)model {
  244. [self loadAssets:model];
  245. [self setTitleLabelText:model.name];
  246. [self hidenGroupView];
  247. }
  248. //加载图片
  249. - (void)loadAssets:(TZAlbumModel *)model {
  250. [self.indexPathsForSelectedItems removeAllObjects];
  251. [self.assets removeAllObjects];
  252. [[TZImageManager manager] getAssetsFromFetchResult:model.result completion:^(NSArray<TZAssetModel *> *models) {
  253. self->_assets = [NSMutableArray arrayWithArray:models];
  254. [self.photoListView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
  255. [self.photoListView reloadData];
  256. }];
  257. }
  258. #pragma mark - uicollectionDelegate
  259. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  260. return self.assets.count;
  261. }
  262. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  263. static NSString *cellIdentifer = @"cell";
  264. AJPhotoListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
  265. BOOL isSelected = [self.indexPathsForSelectedItems containsObject:self.assets[indexPath.row]];
  266. [cell bind:self.assets[indexPath.row] selectionFilter:self.selectionFilter isSelected:isSelected];
  267. KWeakSelf
  268. cell.didClckSelectBut = ^(BOOL isSelect) {
  269. [weakSelf handlCellSelectFunBy:indexPath];
  270. };
  271. return cell;
  272. }
  273. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  274. return UIEdgeInsetsMake(5, 5, 5, 5);
  275. }
  276. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  277. CGFloat wh = (collectionView.bounds.size.width - 20)/3.0;
  278. return CGSizeMake(wh, wh);
  279. }
  280. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  281. return 5.0;
  282. }
  283. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  284. return 5.0;
  285. }
  286. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  287. // AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  288. // ALAsset *asset = self.assets[indexPath.row];
  289. PhotoPreviewViewController *vc =[PhotoPreviewViewController new];
  290. vc.assets = _assets;
  291. vc.currentIndex = indexPath.row;
  292. [self.navigationController pushViewController:vc animated:YES];
  293. //相机按钮处理
  294. // if ([asset isKindOfClass:[UIImage class]] && _delegate && [_delegate respondsToSelector:@selector(photoPickerTapCameraAction:)]) {
  295. // [_delegate photoPickerTapCameraAction:self];
  296. // return;
  297. // }
  298. //单选
  299. // if (!self.multipleSelection && self.indexPathsForSelectedItems.count==1) {
  300. // //取消上一个选中
  301. // NSInteger index = [self.assets indexOfObject:self.indexPathsForSelectedItems[0]];
  302. // NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
  303. // AJPhotoListCell *previousCell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  304. // [previousCell isSelected:NO];
  305. // [self.indexPathsForSelectedItems removeAllObjects];
  306. //
  307. // //选中当前的
  308. // [self.indexPathsForSelectedItems addObject:asset];
  309. // [cell isSelected:YES];
  310. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  311. // [_delegate photoPicker:self didDeselectAsset:asset];
  312. // return;
  313. // }
  314. //超出最大限制
  315. // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
  316. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
  317. // [_delegate photoPickerDidMaximum:self];
  318. // return;
  319. // }
  320. //
  321. // //选择过滤
  322. // BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
  323. // if (!selectable) {
  324. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
  325. // [_delegate photoPickerDidSelectionFilter:self];
  326. // return;
  327. // }
  328. //
  329. // //取消选中
  330. // if ([self.indexPathsForSelectedItems containsObject:asset]) {
  331. // [self.indexPathsForSelectedItems removeObject:asset];
  332. // [cell isSelected:NO];
  333. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  334. // [_delegate photoPicker:self didDeselectAsset:asset];
  335. // return;
  336. // }
  337. //
  338. // //选中
  339. // [self.indexPathsForSelectedItems addObject:asset];
  340. // [cell isSelected:YES];
  341. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
  342. // [_delegate photoPicker:self didSelectAsset:asset];
  343. }
  344. #pragma mark 处理点击选中相关
  345. - (void)handlCellSelectFunBy:(NSIndexPath *)indexPath
  346. {
  347. // AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  348. // ALAsset *asset = self.assets[indexPath.row];
  349. //
  350. // //超出最大限制
  351. // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
  352. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
  353. // [_delegate photoPickerDidMaximum:self];
  354. // return;
  355. // }
  356. //
  357. // //选择过滤
  358. // BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
  359. // if (!selectable) {
  360. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
  361. // [_delegate photoPickerDidSelectionFilter:self];
  362. // return;
  363. // }
  364. //
  365. // //取消选中
  366. // if ([self.indexPathsForSelectedItems containsObject:asset]) {
  367. // [self.indexPathsForSelectedItems removeObject:asset];
  368. // [cell isSelected:NO];
  369. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  370. // [_delegate photoPicker:self didDeselectAsset:asset];
  371. // return;
  372. // }
  373. //
  374. // //选中
  375. // [self.indexPathsForSelectedItems addObject:asset];
  376. // [cell isSelected:YES];
  377. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
  378. // [_delegate photoPicker:self didSelectAsset:asset];
  379. }
  380. #pragma mark - getter/setter
  381. - (NSMutableArray *)assets {
  382. if (!_assets) {
  383. _assets = [[NSMutableArray alloc] init];
  384. }
  385. return _assets;
  386. }
  387. - (NSMutableArray *)indexPathsForSelectedItems {
  388. if (!_indexPathsForSelectedItems) {
  389. _indexPathsForSelectedItems = [[NSMutableArray alloc] init];
  390. }
  391. return _indexPathsForSelectedItems;
  392. }
  393. @end