uploadImageOrVideoViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. @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 = 30;
  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. photoGroupView.isPhotoType = _isPhotoType;
  184. [self.view insertSubview:photoGroupView belowSubview:self.navBarBGView];
  185. self.photoGroupView = photoGroupView;
  186. photoGroupView.hidden = YES;
  187. photoGroupView.translatesAutoresizingMaskIntoConstraints = NO;
  188. [self.photoGroupView setupGroup];
  189. [self delayedSetPhotoGroupViewFrameFun];
  190. }
  191. #pragma mark 延时设置photoGroupView frame
  192. - (void)delayedSetPhotoGroupViewFrameFun
  193. {
  194. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  195. NSInteger maxCount = 6;
  196. if(self.photoGroupView.albumGroups.count < maxCount){
  197. maxCount = self.photoGroupView.albumGroups.count;
  198. }
  199. [self.photoGroupView mas_remakeConstraints:^(MASConstraintMaker *make) {
  200. make.left.mas_equalTo(0);
  201. make.right.mas_equalTo(0);
  202. make.height.mas_equalTo(60.0 * maxCount);
  203. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  204. }];
  205. });
  206. }
  207. #pragma mark - 相册切换
  208. - (void)selectGroupAction:(UIButton *)sender {
  209. //无权限
  210. if (self.isNotAllowed) {
  211. return;
  212. }
  213. if (self.photoGroupView.hidden) {
  214. [self bgMaskView];
  215. self.bgMaskView.hidden = NO;
  216. self.photoGroupView.hidden = NO;
  217. [UIView animateWithDuration:0.3 animations:^{
  218. CGRect rect = self.photoGroupView.frame;
  219. rect.origin.y = CGRectGetMaxY(self.navBarBGView.frame);
  220. self.photoGroupView.frame = rect;
  221. self.selectTip.transform = CGAffineTransformMakeRotation(M_PI);
  222. }];
  223. } else {
  224. [self hidenGroupView];
  225. }
  226. }
  227. - (void)hidenGroupView {
  228. //[self.bgMaskView removeFromSuperview];
  229. self.bgMaskView.hidden = YES;
  230. [UIView animateWithDuration:0.3 animations:^{
  231. CGRect rect = self.photoGroupView.frame;
  232. rect.origin.y = -SCREEN_H;
  233. self.photoGroupView.frame = rect;
  234. self.selectTip.transform = CGAffineTransformIdentity;
  235. }completion:^(BOOL finished) {
  236. self.photoGroupView.hidden = YES;
  237. }];
  238. }
  239. #pragma mark - 遮罩背景
  240. - (UIView *)bgMaskView {
  241. if (_bgMaskView == nil) {
  242. UIView *bgMaskView = [[UIView alloc] init];
  243. bgMaskView.alpha = 0.5;
  244. bgMaskView.translatesAutoresizingMaskIntoConstraints = NO;
  245. bgMaskView.backgroundColor = [UIColor blackColor];
  246. //[self.view insertSubview:bgMaskView aboveSubview:self.photoListView];
  247. [self.view insertSubview:bgMaskView belowSubview:self.photoGroupView];
  248. bgMaskView.userInteractionEnabled = YES;
  249. [bgMaskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgMaskView:)]];
  250. _bgMaskView = bgMaskView;
  251. [bgMaskView mas_makeConstraints:^(MASConstraintMaker *make) {
  252. make.left.mas_equalTo(0);
  253. make.right.mas_equalTo(0);
  254. make.bottom.mas_equalTo(0);
  255. make.top.equalTo(self.navBarBGView.mas_bottom).offset(0.f);
  256. }];
  257. }
  258. return _bgMaskView;
  259. }
  260. - (void)tapBgMaskView:(UITapGestureRecognizer *)sender {
  261. if (!self.photoGroupView.hidden) {
  262. [self hidenGroupView];
  263. }
  264. }
  265. #pragma mark - BoPhotoGroupViewProtocol
  266. - (void)didSelectGroup:(TZAlbumModel *)model {
  267. [self loadAssets:model];
  268. [self setTitleLabelText:model.name];
  269. [self hidenGroupView];
  270. }
  271. //加载图片
  272. - (void)loadAssets:(TZAlbumModel *)model {
  273. [self.indexPathsForSelectedItems removeAllObjects];
  274. [self.assets removeAllObjects];
  275. [[TZImageManager manager] getAssetsFromFetchResult:model.result completion:^(NSArray<TZAssetModel *> *models) {
  276. self->_assets = [NSMutableArray arrayWithArray:models];
  277. [self.photoListView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
  278. [self.photoListView reloadData];
  279. }];
  280. }
  281. #pragma mark - uicollectionDelegate
  282. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  283. return self.assets.count;
  284. }
  285. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  286. static NSString *cellIdentifer = @"cell";
  287. AJPhotoListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
  288. BOOL isSelected = [self.indexPathsForSelectedItems containsObject:self.assets[indexPath.row]];
  289. [cell bind:self.assets[indexPath.row] selectionFilter:self.selectionFilter isSelected:isSelected];
  290. KWeakSelf
  291. cell.didClckSelectBut = ^(BOOL isSelect) {
  292. [weakSelf handlCellSelectFunBy:indexPath];
  293. };
  294. return cell;
  295. }
  296. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  297. return UIEdgeInsetsMake(5, 5, 5, 5);
  298. }
  299. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  300. CGFloat wh = (collectionView.bounds.size.width - 20)/3.0;
  301. return CGSizeMake(wh, wh);
  302. }
  303. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  304. return 5.0;
  305. }
  306. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  307. return 5.0;
  308. }
  309. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  310. PhotoPreviewViewController *vc =[PhotoPreviewViewController new];
  311. vc.assets = _assets;
  312. vc.currentIndex = indexPath.row;
  313. vc.indexPathsForSelectedItems = _indexPathsForSelectedItems;
  314. vc.maximumNumberOfSelection = _maximumNumberOfSelection;
  315. vc.minimumNumberOfSelection = _minimumNumberOfSelection;
  316. [self.navigationController pushViewController:vc animated:YES];
  317. }
  318. #pragma mark 处理点击选中相关
  319. - (void)handlCellSelectFunBy:(NSIndexPath *)indexPath
  320. {
  321. AJPhotoListCell *cell = (AJPhotoListCell *)[self.photoListView cellForItemAtIndexPath:indexPath];
  322. TZAssetModel *model = self.assets[indexPath.row];
  323. //超出最大限制
  324. if (self.indexPathsForSelectedItems.count >= self.maximumNumberOfSelection ) {
  325. return;
  326. }
  327. //取消选中
  328. if ([self.indexPathsForSelectedItems containsObject:model]) {
  329. [self.indexPathsForSelectedItems removeObject:model];
  330. model.isSelected = NO;
  331. [cell isSelected:NO];
  332. }
  333. else{//选中
  334. [self.indexPathsForSelectedItems addObject:model];
  335. model.isSelected = YES;
  336. [cell isSelected:YES];
  337. if(!model.imageData)
  338. {
  339. [[PHImageManager defaultManager] requestImageDataForAsset:model.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  340. // 直接得到最终的 NSData 数据
  341. if (imageData) {
  342. model.imageData = imageData;
  343. }
  344. if(model.type != TZAssetModelMediaTypeVideo){
  345. [self setDataToBottomViewFun];
  346. }
  347. }];
  348. if(model.type == TZAssetModelMediaTypeVideo){
  349. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  350. options.version = PHVideoRequestOptionsVersionOriginal;
  351. [[PHImageManager defaultManager] requestAVAssetForVideo:model.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  352. if ([asset isKindOfClass:[AVURLAsset class]]) {
  353. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  354. NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  355. // NSNumber *size;
  356. // [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
  357. // NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0));
  358. model.videoData = videoData;
  359. [self setDataToBottomViewFun];
  360. }
  361. }];
  362. }
  363. }
  364. }
  365. [self setDataToBottomViewFun];
  366. }
  367. #pragma mark 同步数据到底部
  368. - (void)setDataToBottomViewFun
  369. {
  370. mainBlock(^{
  371. self.uploadFileBottomV.indexPathsForSelectedItems = self.indexPathsForSelectedItems;
  372. });
  373. }
  374. #pragma mark - getter/setter
  375. - (NSMutableArray *)assets {
  376. if (!_assets) {
  377. _assets = [[NSMutableArray alloc] init];
  378. }
  379. return _assets;
  380. }
  381. - (NSMutableArray *)indexPathsForSelectedItems {
  382. if (!_indexPathsForSelectedItems) {
  383. _indexPathsForSelectedItems = [[NSMutableArray alloc] init];
  384. }
  385. return _indexPathsForSelectedItems;
  386. }
  387. #pragma mark 跳转上传记录
  388. - (void)gotoUploadFileRecordFun
  389. {
  390. uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
  391. [self.navigationController pushViewController:vc animated:YES];
  392. [vc gotoUploadFile:_indexPathsForSelectedItems];
  393. }
  394. @end