uploadImageOrVideoViewController.m 17 KB

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