uploadImageOrVideoViewController.m 16 KB

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