uploadImageOrVideoViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. @interface uploadImageOrVideoViewController ()<AJPhotoGroupViewProtocol,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  15. @property (strong, nonatomic) AJPhotoGroupView *photoGroupView;
  16. @property (strong, nonatomic) UILabel *MytitleLabel;
  17. @property (strong, nonatomic) UIImageView *selectTip;
  18. @property (nonatomic) BOOL isNotAllowed;
  19. @property (strong, nonatomic) UIView *bgMaskView;
  20. @property (strong, nonatomic) AJPhotoListView *photoListView;
  21. @property (strong, nonatomic) NSMutableArray *assets;
  22. @property (strong, nonatomic) NSIndexPath *lastAccessed;
  23. @property (strong, nonatomic) uploadFileBottomView *uploadFileBottomV;
  24. //最多选择项
  25. @property (nonatomic, assign) NSInteger maximumNumberOfSelection;
  26. //最少选择项
  27. @property (nonatomic, assign) NSInteger minimumNumberOfSelection;
  28. //是否开启多选
  29. @property (nonatomic, assign) BOOL multipleSelection;
  30. @end
  31. @implementation uploadImageOrVideoViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. [self.view setBackgroundColor:HWF5F7FAColor];
  36. [self.toolBar setHidden:YES];
  37. [self.navigationBar setHidden:YES];
  38. [self.navBarBGView setHidden:NO];
  39. [self initBaselUIFun];
  40. _isNotAllowed = YES;
  41. _maximumNumberOfSelection = 3;
  42. _minimumNumberOfSelection = 0;
  43. _multipleSelection = YES;
  44. if(!_selectionFilter)
  45. {
  46. _selectionFilter = [NSPredicate predicateWithValue:YES];
  47. }
  48. [self checkVideoAccessFun];
  49. }
  50. - (void)initBaselUIFun
  51. {
  52. [self initNavHeadUIFun];
  53. //列表view
  54. [self setupPhotoListView];
  55. [self setupUploadFileBottomView];
  56. //相册分组
  57. [self setupGroupView];
  58. }
  59. - (void)initNavHeadUIFun
  60. {
  61. //title
  62. UILabel *titleLabel = [[UILabel alloc] init];
  63. titleLabel.textAlignment = NSTextAlignmentCenter;
  64. titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
  65. titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.navBarBGView addSubview:titleLabel];
  67. self.MytitleLabel = titleLabel;
  68. //selectTipImageView
  69. UIImageView *selectTip = [[UIImageView alloc] init];
  70. selectTip.image = [UIImage imageNamed:@"upload_image_arrow"];
  71. selectTip.translatesAutoresizingMaskIntoConstraints = NO;
  72. [self.navBarBGView addSubview:selectTip];
  73. self.selectTip = selectTip;
  74. UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  75. tapBtn.backgroundColor = [UIColor clearColor];
  76. tapBtn.translatesAutoresizingMaskIntoConstraints = NO;
  77. [tapBtn addTarget:self action:@selector(selectGroupAction:) forControlEvents:UIControlEventTouchUpInside];
  78. [self.navBarBGView addSubview:tapBtn];
  79. [tapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.centerX.mas_equalTo(self.navBarBGView.mas_centerX);
  81. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  82. make.width.mas_equalTo(100);
  83. make.height.mas_equalTo(30);
  84. }];
  85. [self setTitleLabelText:@"所有相片"];
  86. }
  87. #pragma mark 设置相册标题
  88. - (void)setTitleLabelText:(NSString*)title
  89. {
  90. CGFloat curWidth = [title boundingRectWithSize:CGSizeMake(SCREEN_W, 30) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.f]} context:nil].size.width;
  91. self.MytitleLabel.text = title;
  92. curWidth += 20;
  93. //HLog(@"title w:%f",curWidth);
  94. [self.MytitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  95. make.centerX.mas_equalTo(self.navBarBGView.mas_centerX).offset(-10);
  96. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  97. make.width.mas_equalTo(curWidth);
  98. make.height.mas_equalTo(30);
  99. }];
  100. //self.MytitleLabel.backgroundColor = [UIColor greenColor];
  101. [self.selectTip mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.left.mas_equalTo(self.MytitleLabel.mas_right).offset(2);
  103. make.centerY.mas_equalTo(self.backBtn.mas_centerY);
  104. make.width.mas_equalTo(15);
  105. make.height.mas_equalTo(15);
  106. }];
  107. }
  108. #pragma mark 判断手机相册权限
  109. - (void)checkVideoAccessFun
  110. {
  111. AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  112. //判断摄像头状态是否可用
  113. if(authStatus==AVAuthorizationStatusAuthorized){
  114. _isNotAllowed = NO;
  115. }else{
  116. NSLog(@"未开启相机权限,请前往设置中开启");
  117. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  118. if (granted){
  119. self->_isNotAllowed = NO;
  120. // mainBlock(^{
  121. //
  122. // });
  123. }
  124. }];
  125. }
  126. }
  127. /**
  128. * 照片列表
  129. */
  130. - (void)setupPhotoListView {
  131. AJPhotoListView *collectionView = [[AJPhotoListView alloc] init];
  132. collectionView.dataSource = self;
  133. collectionView.delegate = self;
  134. collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  135. [self.view insertSubview:collectionView atIndex:0];
  136. self.photoListView = collectionView;
  137. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.left.mas_equalTo(0);
  139. make.right.mas_equalTo(0);
  140. make.bottom.mas_equalTo(-(60 + safeArea));
  141. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  142. }];
  143. }
  144. /**
  145. * 照片列表
  146. */
  147. - (void)setupUploadFileBottomView
  148. {
  149. uploadFileBottomView *bottomView = [[uploadFileBottomView alloc] init];
  150. [self.view insertSubview:bottomView atIndex:0];
  151. self.uploadFileBottomV = bottomView;
  152. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.left.mas_equalTo(0);
  154. make.right.mas_equalTo(0);
  155. make.bottom.mas_equalTo(0);
  156. make.height.mas_equalTo(60 + safeArea);
  157. }];
  158. }
  159. /**
  160. * 相册
  161. */
  162. - (void)setupGroupView {
  163. AJPhotoGroupView *photoGroupView = [[AJPhotoGroupView alloc] init];
  164. //photoGroupView.assetsFilter = self.assetsFilter;
  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. //相机按钮处理
  290. // if ([asset isKindOfClass:[UIImage class]] && _delegate && [_delegate respondsToSelector:@selector(photoPickerTapCameraAction:)]) {
  291. // [_delegate photoPickerTapCameraAction:self];
  292. // return;
  293. // }
  294. //单选
  295. // if (!self.multipleSelection && self.indexPathsForSelectedItems.count==1) {
  296. // //取消上一个选中
  297. // NSInteger index = [self.assets indexOfObject:self.indexPathsForSelectedItems[0]];
  298. // NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
  299. // AJPhotoListCell *previousCell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  300. // [previousCell isSelected:NO];
  301. // [self.indexPathsForSelectedItems removeAllObjects];
  302. //
  303. // //选中当前的
  304. // [self.indexPathsForSelectedItems addObject:asset];
  305. // [cell isSelected:YES];
  306. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  307. // [_delegate photoPicker:self didDeselectAsset:asset];
  308. // return;
  309. // }
  310. //超出最大限制
  311. // if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
  312. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
  313. // [_delegate photoPickerDidMaximum:self];
  314. // return;
  315. // }
  316. //
  317. // //选择过滤
  318. // BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
  319. // if (!selectable) {
  320. // if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
  321. // [_delegate photoPickerDidSelectionFilter:self];
  322. // return;
  323. // }
  324. //
  325. // //取消选中
  326. // if ([self.indexPathsForSelectedItems containsObject:asset]) {
  327. // [self.indexPathsForSelectedItems removeObject:asset];
  328. // [cell isSelected:NO];
  329. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  330. // [_delegate photoPicker:self didDeselectAsset:asset];
  331. // return;
  332. // }
  333. //
  334. // //选中
  335. // [self.indexPathsForSelectedItems addObject:asset];
  336. // [cell isSelected:YES];
  337. // if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
  338. // [_delegate photoPicker:self didSelectAsset:asset];
  339. }
  340. #pragma mark 处理点击选中相关
  341. - (void)handlCellSelectFunBy:(NSIndexPath *)indexPath
  342. {
  343. AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  344. ALAsset *asset = self.assets[indexPath.row];
  345. //超出最大限制
  346. if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection && ![self.indexPathsForSelectedItems containsObject:asset]) {
  347. if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidMaximum:)])
  348. [_delegate photoPickerDidMaximum:self];
  349. return;
  350. }
  351. //选择过滤
  352. BOOL selectable = [self.selectionFilter evaluateWithObject:asset];
  353. if (!selectable) {
  354. if (_delegate && [_delegate respondsToSelector:@selector(photoPickerDidSelectionFilter:)])
  355. [_delegate photoPickerDidSelectionFilter:self];
  356. return;
  357. }
  358. //取消选中
  359. if ([self.indexPathsForSelectedItems containsObject:asset]) {
  360. [self.indexPathsForSelectedItems removeObject:asset];
  361. [cell isSelected:NO];
  362. if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didDeselectAsset:)])
  363. [_delegate photoPicker:self didDeselectAsset:asset];
  364. return;
  365. }
  366. //选中
  367. [self.indexPathsForSelectedItems addObject:asset];
  368. [cell isSelected:YES];
  369. if (_delegate && [_delegate respondsToSelector:@selector(photoPicker:didSelectAsset:)])
  370. [_delegate photoPicker:self didSelectAsset:asset];
  371. }
  372. #pragma mark - getter/setter
  373. - (NSMutableArray *)assets {
  374. if (!_assets) {
  375. _assets = [[NSMutableArray alloc] init];
  376. }
  377. return _assets;
  378. }
  379. - (NSMutableArray *)indexPathsForSelectedItems {
  380. if (!_indexPathsForSelectedItems) {
  381. _indexPathsForSelectedItems = [[NSMutableArray alloc] init];
  382. }
  383. return _indexPathsForSelectedItems;
  384. }
  385. @end