YBPublishCoverVC.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // YBPublishCoverVC.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/9/16.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "YBPublishCoverVC.h"
  9. #import <TXLiteAVSDK_Professional/TXVideoEditer.h>
  10. @interface YBPublishCoverVC ()
  11. {
  12. BOOL _isContinue;
  13. int _imageNum;
  14. NSMutableArray *_imageList;
  15. NSMutableArray *_btnArray;
  16. }
  17. @property(nonatomic,strong)UIImageView *topCoverIV;
  18. @property(nonatomic,strong)UIScrollView *scrollView;
  19. @property(nonatomic,strong)UIView *botView;
  20. @end
  21. @implementation YBPublishCoverVC
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.naviView.hidden = YES;
  25. self.view.backgroundColor = [UIColor blackColor];
  26. [self.view addSubview:self.topCoverIV];
  27. [self.view addSubview:self.scrollView];
  28. [self.view addSubview:self.botView];
  29. [_topCoverIV mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.width.equalTo(self.view.mas_width).offset(-80);
  31. make.centerX.equalTo(self.view);
  32. make.top.equalTo(self.view.mas_top).offset(statusbarHeight+40);
  33. }];
  34. [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.height.mas_equalTo(70);
  36. make.width.equalTo(self.view.mas_width).offset(-20);
  37. make.centerX.equalTo(self.view);
  38. make.top.equalTo(_topCoverIV.mas_bottom).offset(17);
  39. }];
  40. [_botView mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.height.mas_equalTo(50);
  42. make.top.equalTo(_scrollView.mas_bottom).offset(15);
  43. make.bottom.equalTo(self.view.mas_bottom).offset(-ShowDiff-10);
  44. make.width.centerX.equalTo(self.view);
  45. }];
  46. _btnArray = [NSMutableArray array];
  47. NSURL *avUrl = [NSURL fileURLWithPath:_videoPath];
  48. self.videoAsset = [AVAsset assetWithURL:avUrl];
  49. }
  50. - (void)setVideoAsset:(AVAsset *)videoAsset {
  51. _videoAsset = videoAsset;
  52. [self getVdieoCoverList];
  53. }
  54. -(void)getVdieoCoverList {
  55. //显示微缩图列表
  56. _imageList = [NSMutableArray new];
  57. _imageNum = 12;
  58. _isContinue = YES;
  59. [TXVideoInfoReader getSampleImages:_imageNum videoAsset:_videoAsset progress:^BOOL(int number, UIImage *image) {
  60. if (!_isContinue || !image) {
  61. return NO;
  62. }else{
  63. dispatch_async(dispatch_get_main_queue(), ^{
  64. if (!_isContinue) {
  65. return;
  66. }
  67. if (number == 1) {
  68. for (int i = 0; i < _imageNum; i++) {
  69. [_imageList addObject:image];
  70. }
  71. [self setUpListUI];
  72. } else {
  73. if (_imageList.count == 0) {
  74. for (int i = 0; i < _imageNum; i++) {
  75. [_imageList addObject:image];
  76. }
  77. [self setUpListUI];
  78. }
  79. _imageList[number-1] = image;
  80. }
  81. });
  82. return YES;
  83. }
  84. }];
  85. }
  86. -(void)setUpListUI {
  87. NSLog(@"=======:%@===%lu",_imageList,(unsigned long)_imageList.count);
  88. [_scrollView removeAllSubViews];
  89. [_btnArray removeAllObjects];
  90. _scrollView.contentSize = CGSizeMake(50*_imageList.count, 0);
  91. MASViewAttribute *leftMas = _scrollView.mas_left;
  92. for (int i=0; i<_imageList.count; i++) {
  93. UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  94. coverBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
  95. coverBtn.imageView.clipsToBounds = YES;
  96. coverBtn.imageEdgeInsets = UIEdgeInsetsMake(1, 1, 1, 1);
  97. [coverBtn setImage:_imageList[i] forState:0];
  98. coverBtn.tag = 10086+i;
  99. [coverBtn addTarget:self action:@selector(clickCoverList:) forControlEvents:UIControlEventTouchUpInside];
  100. [_scrollView addSubview:coverBtn];
  101. [_btnArray addObject:coverBtn];
  102. [coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.equalTo(leftMas);
  104. make.width.mas_equalTo(50);
  105. make.height.mas_equalTo(70);
  106. make.centerY.equalTo(_scrollView);
  107. }];
  108. leftMas = coverBtn.mas_right;
  109. coverBtn.layer.borderWidth = 2;
  110. coverBtn.layer.borderColor = UIColor.clearColor.CGColor;
  111. if (_selIndex == i) {
  112. coverBtn.layer.borderColor = Pink_Cor.CGColor;
  113. _topCoverIV.image = _imageList[i];
  114. }
  115. }
  116. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  117. for (int i=0; i<_imageList.count; i++) {
  118. /*
  119. UIImage *image = _imageList[i];
  120. CGFloat fixelW = CGImageGetWidth(image.CGImage);
  121. CGFloat fixelH = CGImageGetHeight(image.CGImage);
  122. NSLog(@"rkrate:======:%f====%f:=====%.2f",fixelW,fixelH,fixelH/fixelW);
  123. */
  124. UIButton *cBtn = _btnArray[i];
  125. [cBtn setImage:_imageList[i] forState:0];
  126. }
  127. _topCoverIV.image = _imageList[_selIndex];
  128. });
  129. }
  130. -(void)clickCoverList:(UIButton *)sender {
  131. for (UIButton *btn in _btnArray) {
  132. btn.layer.borderColor = UIColor.clearColor.CGColor;
  133. }
  134. sender.layer.borderColor = Pink_Cor.CGColor;
  135. _selIndex = (int)sender.tag-10086;
  136. _topCoverIV.image = _imageList[_selIndex];
  137. }
  138. - (UIImageView *)topCoverIV {
  139. if (!_topCoverIV) {
  140. _topCoverIV = [[UIImageView alloc]init];
  141. _topCoverIV.contentMode = UIViewContentModeScaleAspectFit;
  142. }
  143. return _topCoverIV;;
  144. }
  145. - (UIScrollView *)scrollView {
  146. if (!_scrollView) {
  147. _scrollView = [[UIScrollView alloc]init];
  148. }
  149. return _scrollView;
  150. }
  151. - (UIView *)botView {
  152. if (!_botView) {
  153. _botView = [[UIView alloc]init];
  154. UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  155. [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
  156. [closeBtn setImage:[UIImage imageNamed:@"封面-关闭"] forState:0];
  157. [_botView addSubview:closeBtn];
  158. [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  159. make.width.height.mas_equalTo(40);
  160. make.left.equalTo(_botView.mas_left).offset(20);
  161. make.centerY.equalTo(_botView);
  162. }];
  163. UILabel *titleL = [[UILabel alloc]init];
  164. titleL.font = SYS_Font(14);
  165. titleL.textColor = [UIColor whiteColor];
  166. titleL.text = YZMsg(@"封面");
  167. [_botView addSubview:titleL];
  168. [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
  169. make.centerX.centerY.equalTo(_botView);
  170. }];
  171. UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  172. [finishBtn addTarget:self action:@selector(clickFinishBtn) forControlEvents:UIControlEventTouchUpInside];
  173. [finishBtn setImage:[UIImage imageNamed:@"封面-完成"] forState:0];
  174. [_botView addSubview:finishBtn];
  175. [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  176. make.width.height.mas_equalTo(40);
  177. make.right.equalTo(_botView.mas_right).offset(-20);
  178. make.centerY.equalTo(_botView);
  179. }];
  180. }
  181. return _botView;
  182. }
  183. -(void)clickCloseBtn {
  184. [self dismissViewControllerAnimated:YES completion:nil];
  185. }
  186. -(void)clickFinishBtn {
  187. if (_selIndex >= _imageList.count) {
  188. [MBProgressHUD showPop:YZMsg(@"未获取到封面")];
  189. return;
  190. }
  191. if (self.coverEvent) {
  192. self.coverEvent(_selIndex, _imageList[_selIndex]);
  193. }
  194. [self dismissViewControllerAnimated:YES completion:nil];
  195. }
  196. @end