PCSPreviewViewController.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //
  2. // PCSPreviewViewController.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by lr on 2023/3/11.
  6. //
  7. #import "PCSPreviewViewController.h"
  8. #import <Masonry/Masonry.h>
  9. #import "QuitMultipleModeAlertViewController.h"
  10. #import "UIImage+name.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <AVKit/AVKit.h>
  13. @interface PCSPreCollectionCell : UICollectionViewCell
  14. @property (nonatomic) LenzResourceItemModel *model;
  15. @property (nonatomic) UIImageView *imageView;
  16. @property (nonatomic) UIImageView *videoImageView;
  17. @property (nonatomic) AVPlayerViewController *player;
  18. @property (nonatomic) BOOL isPlayer;
  19. @end
  20. @implementation PCSPreCollectionCell
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. if (self = [super initWithFrame:frame]) {
  23. [self.contentView addSubview:self.imageView];
  24. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  25. make.edges.mas_offset(0);
  26. }];
  27. [self.contentView addSubview:self.videoImageView];
  28. [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.center.mas_equalTo(self.contentView);
  30. make.width.height.mas_offset(48);
  31. }];
  32. // player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  33. // player.view.hidden = YES;
  34. [self.contentView addSubview:self.player.view];
  35. }
  36. return self;
  37. }
  38. - (void)setModel:(LenzResourceItemModel *)model {
  39. _model = model;
  40. self.imageView.image = model.image;
  41. if (model.mode == SDK_CAPTURE_MODE_MOVIE) {
  42. self.videoImageView.hidden = NO;
  43. } else {
  44. self.videoImageView.hidden = YES;
  45. }
  46. }
  47. - (void)setIsPlayer:(BOOL)isPlayer {
  48. if (isPlayer) {
  49. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:_model.path]];
  50. self.player.player = player;
  51. [self.player.player play];
  52. self.player.view.hidden = NO;
  53. } else {
  54. [self.player.player pause];
  55. self.player.view.hidden = YES;
  56. }
  57. }
  58. - (void)layoutSubviews {
  59. [super layoutSubviews];
  60. self.player.view.frame = self.contentView.bounds;
  61. }
  62. - (UIImageView *)imageView {
  63. if (!_imageView) {
  64. _imageView = [[UIImageView alloc]init];
  65. _imageView.layer.cornerRadius = 8;
  66. _imageView.layer.masksToBounds = YES;
  67. _imageView.backgroundColor = [UIColor blackColor];
  68. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  69. }
  70. return _imageView;
  71. }
  72. - (UIImageView *)videoImageView {
  73. if (!_videoImageView) {
  74. _videoImageView = [[UIImageView alloc]init];
  75. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  76. _videoImageView.hidden = YES;
  77. }
  78. return _videoImageView;
  79. }
  80. - (AVPlayerViewController *)player {
  81. if (!_player) {
  82. _player = [[AVPlayerViewController alloc]init];
  83. _player.view.hidden = YES;
  84. _player.videoGravity = AVLayerVideoGravityResizeAspect;
  85. }
  86. return _player;
  87. }
  88. @end
  89. @interface PCSPreviewViewController ()<UIScrollViewDelegate, AVPlayerViewControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
  90. @property (nonatomic) UILabel *countLabel;
  91. @property (nonatomic) UIView *modeView;
  92. @property (nonatomic) UIScrollView *scrollView;
  93. @property (nonatomic) UILabel *curentLabel;
  94. @property (nonatomic) UIView *bottomView;
  95. @property (nonatomic) UIButton *backButton;
  96. @property (nonatomic) UILabel *backLabel;
  97. @property (nonatomic) UIButton *deleteButton;
  98. @property (nonatomic) UILabel *deleteLabel;
  99. @property (nonatomic) UIImageView *lineImageView;
  100. @property (nonatomic) NSInteger currentIndex;
  101. @property (nonatomic) UIImageView *videoImageView;
  102. @property (nonatomic) AVPlayerViewController *lastPlayer;
  103. //@property (nonatomic) NSInteger currentModeDataIndex;
  104. @property (nonatomic) NSMutableArray <AVPlayerViewController *> *allPlayer;
  105. @property (nonatomic) UICollectionView* collectionView;
  106. @property (nonatomic) NSIndexPath *playerIndexPath;
  107. @property (nonatomic) NSIndexPath *currentIndexPath;
  108. @end
  109. @implementation PCSPreviewViewController
  110. - (void)viewDidLoad {
  111. [super viewDidLoad];
  112. self.allPlayer = [NSMutableArray array];
  113. self.view.backgroundColor = [UIColor colorWithRed:60/255.0 green:58/255.0 blue:61/255.0 alpha:1];
  114. [self.view addSubview:self.countLabel];
  115. CGFloat top = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top;
  116. [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.top.mas_offset(top + 20);
  118. make.right.mas_offset(-20);
  119. make.height.mas_offset(44);
  120. }];
  121. [self.view addSubview:self.modeView];
  122. [self.modeView mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.left.mas_offset(0);
  124. make.top.mas_equalTo(self.countLabel);
  125. make.height.mas_equalTo(self.countLabel);
  126. make.right.mas_equalTo(self.countLabel.mas_left).mas_offset(-10);
  127. }];
  128. [self setupModeView];
  129. [self.view addSubview:self.bottomView];
  130. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.left.right.bottom.mas_offset(0);
  132. make.height.mas_offset(150);
  133. }];
  134. [self.bottomView addSubview:self.backLabel];
  135. [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.bottom.mas_offset(-30);
  137. make.centerX.mas_equalTo(self.bottomView).mas_offset(-60);
  138. }];
  139. [self.bottomView addSubview:self.backButton];
  140. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.bottom.mas_equalTo(self.backLabel.mas_top).mas_offset(-20);
  142. make.centerX.mas_equalTo(self.backLabel);
  143. make.width.height.mas_offset(60);
  144. }];
  145. [self.bottomView addSubview:self.deleteLabel];
  146. [self.deleteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  147. make.bottom.mas_offset(-30);
  148. make.centerX.mas_equalTo(self.bottomView).mas_offset(60);
  149. }];
  150. [self.bottomView addSubview:self.deleteButton];
  151. [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
  152. make.bottom.mas_equalTo(self.deleteLabel.mas_top).mas_offset(-20);
  153. make.centerX.mas_equalTo(self.deleteLabel);
  154. make.width.height.mas_offset(60);
  155. }];
  156. UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
  157. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  158. layout.minimumLineSpacing = 0;
  159. layout.minimumInteritemSpacing = 0;
  160. layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - top - 224);
  161. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
  162. self.collectionView.delegate = self;
  163. self.collectionView.dataSource = self;
  164. self.collectionView.pagingEnabled = YES;
  165. self.collectionView.hidden = YES;
  166. // 注册item类型
  167. [self.collectionView registerClass:[PCSPreCollectionCell class] forCellWithReuseIdentifier:@"PCSPreCollectionCell"];
  168. [self.view addSubview:self.collectionView];
  169. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  170. make.left.mas_offset(20);
  171. make.right.mas_offset(-20);
  172. make.top.mas_equalTo(self.countLabel.mas_bottom).mas_offset(10);
  173. make.bottom.mas_equalTo(self.bottomView.mas_top);
  174. }];
  175. self.currentIndexPath = nil;
  176. if (self.model.continousArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_CONTINUOUS) {
  177. self.currentIndex = 1;
  178. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.continousArray.count - 1 inSection:0];
  179. }
  180. if (self.model.movieArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_MOVIE) {
  181. self.currentIndex = 2;
  182. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.movieArray.count - 1 inSection:1];
  183. }
  184. if (self.model.panoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_PANORAMA) {
  185. self.currentIndex = 3;
  186. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.panoramArray.count - 1 inSection:2];
  187. }
  188. if (self.model.aiPanoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  189. self.currentIndex = 4;
  190. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.aiPanoramArray.count - 1 inSection:3];
  191. }
  192. [self changeLabelStatusWith:self.currentIndex];
  193. [self updateCountLabelWith:self.currentIndexPath];
  194. [self.view addSubview:self.curentLabel];
  195. [self.curentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.centerX.mas_equalTo(self.view);
  197. make.width.mas_offset(100);
  198. make.height.mas_offset(32);
  199. make.bottom.mas_equalTo(self.collectionView).mas_offset(16);
  200. }];
  201. }
  202. - (void)viewDidAppear:(BOOL)animated {
  203. [super viewDidAppear:animated];
  204. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  205. self.collectionView.hidden = NO;
  206. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  207. CGRect rect = attributes.frame;
  208. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  209. });
  210. }
  211. - (UIImageView *)lineImageView {
  212. if (!_lineImageView) {
  213. _lineImageView = [[UIImageView alloc]init];
  214. _lineImageView.image = [UIImage loadNamed:@"icon_preview_line"];
  215. }
  216. return _lineImageView;
  217. }
  218. - (void)updateCountLabelWith:(NSIndexPath *)indexPath {
  219. if (!indexPath) {
  220. return;
  221. }
  222. if (indexPath.section == 0) {
  223. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.continousArray.count];
  224. } else if (indexPath.section == 1) {
  225. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld条", self.currentIndexPath.row + 1, self.model.movieArray.count];
  226. } else if (indexPath.section == 2) {
  227. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.panoramArray.count];
  228. } else {
  229. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.aiPanoramArray.count];
  230. }
  231. }
  232. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  233. return 4;
  234. }
  235. //返回每个分区的item个数
  236. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  237. if (section == 0) {
  238. return self.model.continousArray.count;
  239. } else if (section == 1) {
  240. return self.model.movieArray.count;
  241. } else if (section == 2) {
  242. return self.model.panoramArray.count;
  243. } else {
  244. return self.model.aiPanoramArray.count;
  245. }
  246. }
  247. //返回每个item
  248. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  249. PCSPreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCSPreCollectionCell" forIndexPath:indexPath];
  250. cell.isPlayer = NO;
  251. if (indexPath.section == 0) {
  252. if (self.model.continousArray.count > indexPath.row) {
  253. LenzResourceItemModel *model = self.model.continousArray[indexPath.row];
  254. cell.model = model;
  255. }
  256. } else if (indexPath.section == 1) {
  257. if (self.model.movieArray.count > indexPath.row) {
  258. LenzResourceItemModel *model = self.model.movieArray[indexPath.row];
  259. cell.model = model;
  260. cell.isPlayer = indexPath == self.playerIndexPath;
  261. }
  262. } else if (indexPath.section == 2) {
  263. if (self.model.panoramArray.count > indexPath.row) {
  264. LenzResourceItemModel *model = self.model.panoramArray[indexPath.row];
  265. cell.model = model;
  266. }
  267. } else if (indexPath.section == 3) {
  268. if (self.model.aiPanoramArray.count > indexPath.row) {
  269. LenzResourceItemModel *model = self.model.aiPanoramArray[indexPath.row];
  270. cell.model = model;
  271. }
  272. }
  273. return cell;
  274. }
  275. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  276. if (indexPath.section == 1) {
  277. self.playerIndexPath = indexPath;
  278. [self.collectionView reloadData];
  279. }
  280. }
  281. // 监听UIScrollView的滑动停止
  282. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  283. self.playerIndexPath = nil;
  284. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  285. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  286. self.currentIndexPath = currentIndexPath;
  287. [self changeLabelStatusWith:currentIndexPath.section + 1];
  288. [self updateCountLabelWith:currentIndexPath];
  289. }
  290. - (void)updateCurrentIndexPath {
  291. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  292. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  293. self.currentIndexPath = currentIndexPath;
  294. [self changeLabelStatusWith:currentIndexPath.section + 1];
  295. [self updateCountLabelWith:currentIndexPath];
  296. }
  297. - (void)updateViewWhenDelete {
  298. self.playerIndexPath = nil;
  299. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  300. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  301. self.currentIndexPath = currentIndexPath;
  302. [self changeLabelStatusWith:currentIndexPath.section + 1];
  303. [self updateCountLabelWith:currentIndexPath];
  304. }
  305. - (void)setupModeView {
  306. [self.modeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  307. CGFloat margin = [UIScreen mainScreen].bounds.size.width == 375 ? 20 : 30;
  308. UIView *lastView = nil;
  309. NSInteger count = 0;
  310. self.currentIndex = 0;
  311. if (self.model.continousArray.count) {
  312. count += self.model.continousArray.count;
  313. self.currentIndex = 1;
  314. UILabel *label = [[UILabel alloc]init];
  315. label.text = @"连拍";
  316. label.tag = 1;
  317. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  318. [label addGestureRecognizer:tap];
  319. label.userInteractionEnabled = YES;
  320. label.textColor = [UIColor whiteColor];
  321. [self.modeView addSubview:label];
  322. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  323. make.left.mas_offset(20);
  324. make.centerY.mas_equalTo(self.countLabel);
  325. }];
  326. lastView = label;
  327. }
  328. if (self.model.movieArray.count) {
  329. count += self.model.movieArray.count;
  330. if (self.currentIndex == 0) {
  331. self.currentIndex = 2;
  332. }
  333. UILabel *label = [[UILabel alloc]init];
  334. label.text = @"视频";
  335. label.tag = 2;
  336. label.textColor = [UIColor whiteColor];
  337. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  338. [label addGestureRecognizer:tap];
  339. label.userInteractionEnabled = YES;
  340. [self.modeView addSubview:label];
  341. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  342. if (lastView) {
  343. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  344. } else {
  345. make.left.mas_equalTo(20);
  346. }
  347. make.centerY.mas_equalTo(self.countLabel);
  348. }];
  349. lastView = label;
  350. }
  351. if (self.model.panoramArray.count) {
  352. count += self.model.panoramArray.count;
  353. if (self.currentIndex == 0) {
  354. self.currentIndex = 3;
  355. }
  356. UILabel *label = [[UILabel alloc]init];
  357. label.text = @"全景";
  358. label.tag = 3;
  359. label.textColor = [UIColor whiteColor];
  360. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  361. [label addGestureRecognizer:tap];
  362. label.userInteractionEnabled = YES;
  363. [self.modeView addSubview:label];
  364. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  365. if (lastView) {
  366. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  367. } else {
  368. make.left.mas_equalTo(20);
  369. }
  370. make.centerY.mas_equalTo(self.countLabel);
  371. }];
  372. lastView = label;
  373. }
  374. if (self.model.aiPanoramArray.count) {
  375. count += self.model.aiPanoramArray.count;
  376. if (self.currentIndex == 0) {
  377. self.currentIndex = 4;
  378. }
  379. UILabel *label = [[UILabel alloc]init];
  380. label.text = @"智能全景";
  381. label.tag = 4;
  382. label.textColor = [UIColor whiteColor];
  383. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  384. [label addGestureRecognizer:tap];
  385. label.userInteractionEnabled = YES;
  386. [self.modeView addSubview:label];
  387. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  388. if (lastView) {
  389. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  390. } else {
  391. make.left.mas_equalTo(20);
  392. }
  393. make.centerY.mas_equalTo(self.countLabel);
  394. }];
  395. lastView = label;
  396. }
  397. self.countLabel.text = [NSString stringWithFormat:@"共%ld笔数据", count];
  398. [self.modeView addSubview:self.lineImageView];
  399. }
  400. - (void)changeAction:(UITapGestureRecognizer *)tap {
  401. [self.view layoutIfNeeded];
  402. UILabel *label = (UILabel *)tap.view;
  403. self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:label.tag - 1];
  404. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  405. CGRect rect = attributes.frame;
  406. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  407. [self changeLabelStatusWith:label.tag];
  408. [self updateCountLabelWith:self.currentIndexPath];
  409. }
  410. - (void)changeLabelStatusWith:(NSInteger)index {
  411. if(index <=0) {
  412. return;
  413. }
  414. UILabel *label1 = [self.modeView viewWithTag:1];
  415. if (label1) {
  416. label1.textColor = [UIColor whiteColor];
  417. }
  418. UILabel *label2 = [self.modeView viewWithTag:2];
  419. if (label2) {
  420. label2.textColor = [UIColor whiteColor];
  421. }
  422. UILabel *label3 = [self.modeView viewWithTag:3];
  423. if (label3) {
  424. label3.textColor = [UIColor whiteColor];
  425. }
  426. UILabel *label4 = [self.modeView viewWithTag:4];
  427. if (label4) {
  428. label4.textColor = [UIColor whiteColor];
  429. }
  430. UILabel *label = (UILabel *)[self.modeView viewWithTag:index];
  431. if (label) {
  432. label.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  433. [self.lineImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  434. make.top.mas_equalTo(label.mas_bottom);
  435. make.centerX.mas_equalTo(label);
  436. make.width.mas_offset(18);
  437. make.height.mas_offset(9);
  438. }];
  439. }
  440. self.currentIndex = index;
  441. }
  442. - (void)updateScrollWith:(NSInteger)tag {
  443. if (tag > 0) {
  444. NSArray <LenzResourceItemModel *> *source = nil;
  445. if (tag == 1) {
  446. source = self.model.continousArray;
  447. } else if (tag == 2) {
  448. source = self.model.movieArray;
  449. } else if (tag == 3) {
  450. source = self.model.panoramArray;
  451. } else if (tag == 4) {
  452. source = self.model.aiPanoramArray;
  453. }
  454. [self updateScrollViewWith:source];
  455. }
  456. }
  457. - (void)updateScrollViewWith:(NSArray <LenzResourceItemModel *> *)array {
  458. CGFloat width = CGRectGetWidth(self.scrollView.frame);
  459. CGFloat height = CGRectGetHeight(self.scrollView.frame);
  460. [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  461. self.scrollView.pagingEnabled = YES;
  462. self.scrollView.contentSize = CGSizeMake(width * array.count, height);
  463. __block UIImageView *lastImageView = nil;
  464. [array enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  465. UIImageView *imageView = [[UIImageView alloc]init];
  466. imageView.image = obj.image;
  467. imageView.tag = idx;
  468. [self.scrollView addSubview:imageView];
  469. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  470. make.top.mas_offset(0);
  471. make.height.mas_offset(height);
  472. make.width.mas_offset(width);
  473. if (lastImageView) {
  474. make.left.mas_equalTo(lastImageView.mas_right);
  475. } else {
  476. make.left.mas_offset(0);
  477. }
  478. }];
  479. UIImageView *videoImageView = [[UIImageView alloc]init];
  480. videoImageView.image = [UIImage loadNamed:@"icon_video"];
  481. [imageView addSubview:videoImageView];
  482. [videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  483. make.center.mas_equalTo(imageView);
  484. make.width.height.mas_offset(48);
  485. }];
  486. [self.view setNeedsLayout];
  487. [self.view layoutIfNeeded];
  488. if (obj.mode == SDK_CAPTURE_MODE_MOVIE) {
  489. videoImageView.hidden = NO;
  490. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(videoAction:)];
  491. [imageView addGestureRecognizer:tap];
  492. imageView.userInteractionEnabled = YES;
  493. AVPlayerViewController *player = [[AVPlayerViewController alloc]init];
  494. player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  495. player.view.hidden = YES;
  496. [imageView addSubview:player.view];
  497. [self.allPlayer addObject:player];
  498. } else {
  499. videoImageView.hidden = YES;
  500. }
  501. lastImageView = imageView;
  502. }];
  503. self.curentLabel.text = [NSString stringWithFormat:@"第%d/%ld条", 1, array.count];
  504. }
  505. - (void)videoAction:(UITapGestureRecognizer *)tap {
  506. UIImageView *view = (UIImageView *)tap.view;
  507. if (self.model.movieArray.count > view.tag) {
  508. LenzResourceItemModel *model = self.model.movieArray[view.tag];
  509. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.path]];
  510. if (self.lastPlayer) {
  511. [self.lastPlayer.player pause];
  512. self.lastPlayer.player = nil;
  513. self.lastPlayer.view.hidden = YES;
  514. }
  515. AVPlayerViewController *playerVC = self.allPlayer[view.tag];
  516. playerVC.view.hidden = NO;
  517. playerVC.player = player;
  518. [playerVC.player play];
  519. self.lastPlayer = playerVC;
  520. }
  521. }
  522. - (void)backAction {
  523. [self dismissViewControllerAnimated:YES completion:nil];
  524. }
  525. - (void)deleteAction {
  526. NSString *title = @"图片删除后无法恢复,请确认!";
  527. if (self.currentIndex == 2) {
  528. title = @"视频删除后无法恢复,请确认!";
  529. }
  530. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:title leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  531. [alertController dismissViewControllerAnimated:YES completion:nil];
  532. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  533. if (self.currentIndex == 1) {
  534. if (self.model.continousArray.count > self.currentIndexPath.item) {
  535. [self.model.continousArray removeObjectAtIndex:self.currentIndexPath.item];
  536. }
  537. } else if (self.currentIndex == 2) {
  538. if (self.model.movieArray.count > self.currentIndexPath.item) {
  539. [self.model.movieArray removeObjectAtIndex:self.currentIndexPath.item];
  540. }
  541. } else if (self.currentIndex == 3) {
  542. if (self.model.panoramArray.count > self.currentIndexPath.item) {
  543. [self.model.panoramArray removeObjectAtIndex:self.currentIndexPath.item];
  544. }
  545. } else if (self.currentIndex == 4) {
  546. if (self.model.aiPanoramArray.count > self.currentIndexPath.item) {
  547. [self.model.aiPanoramArray removeObjectAtIndex:self.currentIndexPath.item];
  548. }
  549. }
  550. [self setupModeView];
  551. [self scrollViewDidEndDecelerating:self.collectionView];
  552. [self.collectionView reloadData];
  553. [self.view setNeedsLayout];
  554. [self.view layoutIfNeeded];
  555. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  556. [self updateCurrentIndexPath];
  557. });
  558. [self updateViewWhenDelete];
  559. if (self.dataChangeBlock) {
  560. self.dataChangeBlock();
  561. }
  562. if (self.model.continousArray.count == 0 &&
  563. self.model.movieArray.count == 0 &&
  564. self.model.panoramArray.count == 0 &&
  565. self.model.aiPanoramArray.count == 0) {
  566. self.curentLabel.hidden = YES;
  567. [alertController dismissViewControllerAnimated:YES completion:^{
  568. [self dismissViewControllerAnimated:YES completion:nil];
  569. }];
  570. } else {
  571. self.curentLabel.hidden = NO;
  572. [alertController dismissViewControllerAnimated:YES completion:nil];
  573. }
  574. }];
  575. }
  576. - (UILabel *)countLabel {
  577. if (!_countLabel) {
  578. _countLabel = [[UILabel alloc]init];
  579. _countLabel.font = [UIFont systemFontOfSize:14];
  580. _countLabel.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  581. }
  582. return _countLabel;
  583. }
  584. - (UIView *)bottomView {
  585. if (!_bottomView) {
  586. _bottomView = [[UIView alloc]init];
  587. }
  588. return _bottomView;
  589. }
  590. - (UIView *)modeView {
  591. if (!_modeView) {
  592. _modeView = [[UIView alloc]init];
  593. _modeView.userInteractionEnabled = YES;
  594. }
  595. return _modeView;
  596. }
  597. - (UIScrollView *)scrollView {
  598. if (!_scrollView) {
  599. _scrollView = [[UIScrollView alloc]init];
  600. _scrollView.layer.cornerRadius = 8;
  601. _scrollView.layer.masksToBounds = YES;
  602. _scrollView.delegate = self;
  603. _scrollView.backgroundColor = [UIColor blackColor];
  604. }
  605. return _scrollView;
  606. }
  607. - (UIButton *)backButton {
  608. if (!_backButton) {
  609. _backButton = [[UIButton alloc]init];
  610. [_backButton setImage:[UIImage loadNamed:@"result-return-btn"] forState:UIControlStateNormal];
  611. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  612. }
  613. return _backButton;
  614. }
  615. - (UILabel *)backLabel {
  616. if (!_backLabel) {
  617. _backLabel = [[UILabel alloc]init];
  618. _backLabel.text = @"返回";
  619. _backLabel.textColor = [UIColor whiteColor];
  620. }
  621. return _backLabel;
  622. }
  623. - (UILabel *)deleteLabel {
  624. if (!_deleteLabel) {
  625. _deleteLabel = [[UILabel alloc]init];
  626. _deleteLabel.text = @"删除";
  627. _deleteLabel.textColor = [UIColor whiteColor];
  628. }
  629. return _deleteLabel;
  630. }
  631. - (UIButton *)deleteButton {
  632. if (!_deleteButton) {
  633. _deleteButton = [[UIButton alloc]init];
  634. [_deleteButton setImage:[UIImage loadNamed:@"result-delete-btn"] forState:UIControlStateNormal];
  635. [_deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  636. }
  637. return _deleteButton;
  638. }
  639. - (UILabel *)curentLabel {
  640. if (!_curentLabel) {
  641. _curentLabel = [[UILabel alloc]init];
  642. _curentLabel.backgroundColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  643. _curentLabel.layer.cornerRadius = 16;
  644. _curentLabel.layer.masksToBounds = YES;
  645. _curentLabel.textAlignment = NSTextAlignmentCenter;
  646. _curentLabel.textColor = [UIColor whiteColor];
  647. }
  648. return _curentLabel;
  649. }
  650. - (UIImageView *)videoImageView {
  651. if (!_videoImageView) {
  652. _videoImageView = [[UIImageView alloc]init];
  653. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  654. }
  655. return _videoImageView;
  656. }
  657. @end