imageCollectionViewCell.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // imageCollectionViewCell.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/13.
  6. //
  7. #import "imageCollectionViewCell.h"
  8. #import <SDWebImage/SDWebImage.h>
  9. @interface imageCollectionViewCell()
  10. @property (strong, nonatomic) UIImageView *imageView;
  11. @property (nonatomic, strong) UIButton *selectButton;
  12. @property (strong, nonatomic) UILabel *timeLabel;
  13. @end
  14. @implementation imageCollectionViewCell
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. if (self = [super initWithFrame:frame]) {
  17. [self setupSubViews];
  18. }
  19. return self;
  20. }
  21. - (void)setupSubViews{
  22. _imageView = [UIImageView new];
  23. _imageView.backgroundColor = [UIColor lightGrayColor];
  24. [self.contentView addSubview:_imageView];
  25. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.left.mas_equalTo(0);
  27. make.right.mas_equalTo(0);
  28. make.top.mas_equalTo(0);
  29. make.bottom.mas_equalTo(0);
  30. }];
  31. _imageView.userInteractionEnabled = YES;
  32. // 创建长按手势识别器
  33. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  34. // 设置长按手势的最小按压时间
  35. longPress.minimumPressDuration = 1; // 2秒
  36. // 将手势识别器添加到视图上
  37. [_imageView addGestureRecognizer:longPress];
  38. UIButton *but = [[UIButton alloc] init];
  39. [but setImage:[UIImage imageNamed:@"upload_file_uncheck2"] forState:UIControlStateNormal];
  40. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  41. [self.contentView addSubview:but];
  42. [but addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
  43. but.hidden = YES;
  44. //but.backgroundColor = [UIColor greenColor];
  45. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.width.mas_equalTo(30);
  47. make.height.mas_equalTo(30);
  48. make.right.mas_equalTo(0);
  49. make.top.mas_equalTo(0);
  50. }];
  51. self.selectButton = but;
  52. _timeLabel = [[UILabel alloc] init];
  53. _timeLabel.textColor = [UIColor whiteColor];
  54. _timeLabel.font = [UIFont systemFontOfSize:14.0];
  55. _timeLabel.textAlignment = NSTextAlignmentRight;
  56. _timeLabel.hidden = YES;
  57. [_imageView addSubview:_timeLabel];
  58. [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.mas_equalTo(5);
  60. make.right.mas_equalTo(-5);
  61. make.height.mas_equalTo(20);
  62. make.bottom.mas_equalTo(-5);
  63. }];
  64. }
  65. - (void)setCurFileModel:(NASFilePicDataArrModel *)curFileModel
  66. {
  67. _curFileModel = curFileModel;
  68. NSString *filePath = _curFileModel.path;
  69. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  70. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getThumbnail?path=%@",urlStr,filePath];
  71. UIImage *defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  72. if([self.fileType isEqualToString:@"video"])
  73. {
  74. _timeLabel.hidden = NO;
  75. NSInteger curPlayTime = _curFileModel.duration;
  76. NSInteger hourTime = 60*60;
  77. NSInteger minTime = 60;
  78. NSString *playTimeStr = nil;
  79. NSString *playHourTimeStr = nil;
  80. NSString *playMinTimeStr = nil;
  81. NSString *playSecondTimeStr = nil;
  82. if(curPlayTime >= hourTime){
  83. NSInteger hour = curPlayTime/hourTime;
  84. playHourTimeStr = [[NSString alloc] initWithFormat:@"%ld",hour];
  85. curPlayTime = curPlayTime % hourTime;
  86. }
  87. if(curPlayTime >= minTime){
  88. NSInteger min = curPlayTime/minTime;
  89. if(min >=10){
  90. playMinTimeStr = [[NSString alloc] initWithFormat:@"%ld",min];
  91. }
  92. else{
  93. playMinTimeStr = [[NSString alloc] initWithFormat:@"0%ld",min];
  94. }
  95. curPlayTime = curPlayTime % min;
  96. }
  97. else{
  98. playMinTimeStr = @"00";
  99. }
  100. //秒
  101. {
  102. if(curPlayTime >=10){
  103. playSecondTimeStr = [[NSString alloc] initWithFormat:@"%ld",curPlayTime];
  104. }
  105. else{
  106. playSecondTimeStr = [[NSString alloc] initWithFormat:@"0%ld",curPlayTime];
  107. }
  108. }
  109. if(playHourTimeStr){
  110. _timeLabel.text = [[NSString alloc] initWithFormat:@"%@:%@:%@",playHourTimeStr,playMinTimeStr,playSecondTimeStr];
  111. }
  112. else{
  113. _timeLabel.text = [[NSString alloc] initWithFormat:@"%@:%@",playMinTimeStr,playSecondTimeStr];
  114. }
  115. defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  116. }
  117. else{
  118. //iOS格式的图片 代理拿不到缩略图
  119. if([filePath rangeOfString:@".HEIC"].location != NSNotFound
  120. ||[filePath rangeOfString:@".heic"].location != NSNotFound){
  121. fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  122. }
  123. _timeLabel.hidden = YES;
  124. }
  125. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  126. [_imageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  127. // if(image){
  128. // HLog(@"11图片1:%@",imageURL.absoluteString);
  129. // }
  130. // else{
  131. // HLog(@"11图片0:%@",imageURL.absoluteString);
  132. // }
  133. }];
  134. if(!_isEditType){
  135. self.selectButton.hidden = YES;
  136. }
  137. else{
  138. self.selectButton.hidden = NO;
  139. self.selectButton.selected = curFileModel.isSelectType;
  140. }
  141. }
  142. #pragma mark 选中按钮
  143. - (void)didClickButFun:(UIButton*)but
  144. {
  145. but.selected = !but.selected;
  146. if(_didClckSelectBut){
  147. _didClckSelectBut(but.selected);
  148. }
  149. }
  150. - (void)longPress:(UILongPressGestureRecognizer *)gesture {
  151. if (gesture.state == UIGestureRecognizerStateBegan) {
  152. HLog(@"长按手势开始");
  153. if(_didClickEditType && !_isEditType){
  154. _didClickEditType(YES);
  155. }
  156. }
  157. }
  158. @end