imageCollectionViewCell~.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  25. _imageView.layer.masksToBounds = YES;
  26. [self.contentView addSubview:_imageView];
  27. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.left.mas_equalTo(0);
  29. make.right.mas_equalTo(0);
  30. make.top.mas_equalTo(0);
  31. make.bottom.mas_equalTo(0);
  32. }];
  33. _imageView.userInteractionEnabled = YES;
  34. // 创建长按手势识别器
  35. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  36. // 设置长按手势的最小按压时间
  37. longPress.minimumPressDuration = 1; // 2秒
  38. // 将手势识别器添加到视图上
  39. [_imageView addGestureRecognizer:longPress];
  40. UIButton *but = [[UIButton alloc] init];
  41. [but setImage:[UIImage imageNamed:@"upload_file_uncheck2"] forState:UIControlStateNormal];
  42. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  43. [self.contentView addSubview:but];
  44. [but addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
  45. //but.hidden = YES;
  46. //but.backgroundColor = [UIColor greenColor];
  47. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.width.mas_equalTo(30);
  49. make.height.mas_equalTo(30);
  50. make.right.mas_equalTo(0);
  51. make.top.mas_equalTo(0);
  52. }];
  53. self.selectButton = but;
  54. _timeLabel = [[UILabel alloc] init];
  55. _timeLabel.textColor = [UIColor whiteColor];
  56. _timeLabel.font = [UIFont systemFontOfSize:14.0];
  57. _timeLabel.textAlignment = NSTextAlignmentRight;
  58. _timeLabel.hidden = YES;
  59. [_imageView addSubview:_timeLabel];
  60. [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.left.mas_equalTo(5);
  62. make.right.mas_equalTo(-5);
  63. make.height.mas_equalTo(20);
  64. make.bottom.mas_equalTo(-5);
  65. }];
  66. }
  67. - (void)setCurFileModel:(NASFilePicDataArrModel *)curFileModel
  68. {
  69. _curFileModel = curFileModel;
  70. NSString *filePath = _curFileModel.path;
  71. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  72. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getThumbnail?path=%@",urlStr,filePath];
  73. UIImage *defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  74. if([self.fileType isEqualToString:@"video"])
  75. {
  76. _timeLabel.hidden = NO;
  77. NSInteger curPlayTime = _curFileModel.duration;
  78. NSInteger hourTime = 60*60;
  79. NSInteger minTime = 60;
  80. NSString *playTimeStr = nil;
  81. NSString *playHourTimeStr = nil;
  82. NSString *playMinTimeStr = nil;
  83. NSString *playSecondTimeStr = nil;
  84. if(curPlayTime >= hourTime){
  85. NSInteger hour = curPlayTime/hourTime;
  86. playHourTimeStr = [[NSString alloc] initWithFormat:@"%ld",hour];
  87. curPlayTime = curPlayTime % hourTime;
  88. }
  89. if(curPlayTime >= minTime){
  90. NSInteger min = curPlayTime/minTime;
  91. if(min >=10){
  92. playMinTimeStr = [[NSString alloc] initWithFormat:@"%ld",min];
  93. }
  94. else{
  95. playMinTimeStr = [[NSString alloc] initWithFormat:@"0%ld",min];
  96. }
  97. curPlayTime = curPlayTime % min;
  98. }
  99. else{
  100. playMinTimeStr = @"00";
  101. }
  102. //秒
  103. {
  104. if(curPlayTime >=10){
  105. playSecondTimeStr = [[NSString alloc] initWithFormat:@"%ld",curPlayTime];
  106. }
  107. else{
  108. playSecondTimeStr = [[NSString alloc] initWithFormat:@"0%ld",curPlayTime];
  109. }
  110. }
  111. NSString *totalTimeStr = nil;
  112. if(playHourTimeStr){
  113. totalTimeStr = [[NSString alloc] initWithFormat:@"%@:%@:%@",playHourTimeStr,playMinTimeStr,playSecondTimeStr];
  114. }
  115. else{
  116. totalTimeStr = [[NSString alloc] initWithFormat:@"%@:%@",playMinTimeStr,playSecondTimeStr];
  117. }
  118. NSShadow *shadow = [[NSShadow alloc] init];
  119. shadow.shadowBlurRadius = 4;
  120. shadow.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
  121. shadow.shadowOffset =CGSizeMake(0,2);
  122. NSAttributedString * attributedText = [[NSAttributedString alloc] initWithString:totalTimeStr attributes:@{NSShadowAttributeName:shadow}];
  123. _timeLabel.attributedText = attributedText;
  124. defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  125. }
  126. else{
  127. //iOS格式的图片 代理拿不到缩略图
  128. if([filePath rangeOfString:@".HEIC"].location != NSNotFound
  129. ||[filePath rangeOfString:@".heic"].location != NSNotFound){
  130. fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  131. }
  132. _timeLabel.hidden = YES;
  133. }
  134. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  135. [_imageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  136. // if(image){
  137. // HLog(@"11图片1:%@",imageURL.absoluteString);
  138. // }
  139. // else{
  140. // HLog(@"11图片0:%@",imageURL.absoluteString);
  141. // }
  142. }];
  143. // if(!_isEditType){
  144. // self.selectButton.hidden = YES;
  145. // }
  146. // else{
  147. // self.selectButton.hidden = NO;
  148. // self.selectButton.selected = curFileModel.isSelectType;
  149. // }
  150. self.selectButton.selected = curFileModel.isSelectType;
  151. }
  152. #pragma mark 选中按钮
  153. - (void)didClickButFun:(UIButton*)but
  154. {
  155. but.selected = !but.selected;
  156. if(_didClckSelectBut){
  157. _didClckSelectBut(but.selected);
  158. }
  159. }
  160. - (void)longPress:(UILongPressGestureRecognizer *)gesture {
  161. if (gesture.state == UIGestureRecognizerStateBegan) {
  162. HLog(@"长按手势开始");
  163. if(_didClickEditType && !_isEditType){
  164. _didClickEditType(YES);
  165. }
  166. }
  167. }
  168. @end