uploadImageOrVideoViewController.m 15 KB

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