imageCollectionViewCell.m 6.0 KB

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