// // imageCollectionViewCell.m // 双子星云手机 // // Created by xd h on 2024/5/13. // #import "imageCollectionViewCell.h" #import @interface imageCollectionViewCell() @property (strong, nonatomic) UIImageView *imageView; @property (nonatomic, strong) UIButton *selectButton; @property (strong, nonatomic) UILabel *timeLabel; @end @implementation imageCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupSubViews]; } return self; } - (void)setupSubViews{ _imageView = [UIImageView new]; //_imageView.backgroundColor = [UIColor lightGrayColor]; _imageView.contentMode = UIViewContentModeScaleAspectFill; _imageView.layer.masksToBounds = YES; [self.contentView addSubview:_imageView]; [_imageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; _imageView.userInteractionEnabled = YES; // 创建长按手势识别器 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; // 设置长按手势的最小按压时间 longPress.minimumPressDuration = 1; // 2秒 // 将手势识别器添加到视图上 [_imageView addGestureRecognizer:longPress]; UIButton *but = [[UIButton alloc] init]; [but setImage:[UIImage imageNamed:@"upload_file_uncheck2"] forState:UIControlStateNormal]; [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected]; [self.contentView addSubview:but]; [but addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside]; //but.hidden = YES; //but.backgroundColor = [UIColor greenColor]; [but mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(30); make.height.mas_equalTo(30); make.right.mas_equalTo(0); make.top.mas_equalTo(0); }]; self.selectButton = but; _timeLabel = [[UILabel alloc] init]; _timeLabel.textColor = [UIColor whiteColor]; _timeLabel.font = [UIFont systemFontOfSize:14.0]; _timeLabel.textAlignment = NSTextAlignmentRight; _timeLabel.hidden = YES; [_imageView addSubview:_timeLabel]; [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(5); make.right.mas_equalTo(-5); make.height.mas_equalTo(20); make.bottom.mas_equalTo(-5); }]; } - (void)setCurFileModel:(NASFilePicDataArrModel *)curFileModel { _curFileModel = curFileModel; NSString *filePath = _curFileModel.path; NSString *urlStr = ksharedAppDelegate.NASFileByBoxService; NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getThumbnail?path=%@",urlStr,filePath]; UIImage *defaultImage = [UIImage imageNamed:@"uploadFile_image"]; if([self.fileType isEqualToString:@"video"]) { _timeLabel.hidden = NO; NSInteger curPlayTime = _curFileModel.duration; NSInteger hourTime = 60*60; NSInteger minTime = 60; NSString *playTimeStr = nil; NSString *playHourTimeStr = nil; NSString *playMinTimeStr = nil; NSString *playSecondTimeStr = nil; if(curPlayTime >= hourTime){ NSInteger hour = curPlayTime/hourTime; playHourTimeStr = [[NSString alloc] initWithFormat:@"%ld",hour]; curPlayTime = curPlayTime % hourTime; } if(curPlayTime >= minTime){ NSInteger min = curPlayTime/minTime; if(min >=10){ playMinTimeStr = [[NSString alloc] initWithFormat:@"%ld",min]; } else{ playMinTimeStr = [[NSString alloc] initWithFormat:@"0%ld",min]; } curPlayTime = curPlayTime % min; } else{ playMinTimeStr = @"00"; } //秒 { if(curPlayTime >=10){ playSecondTimeStr = [[NSString alloc] initWithFormat:@"%ld",curPlayTime]; } else{ playSecondTimeStr = [[NSString alloc] initWithFormat:@"0%ld",curPlayTime]; } } NSString *totalTimeStr = nil; if(playHourTimeStr){ totalTimeStr = [[NSString alloc] initWithFormat:@"%@:%@:%@",playHourTimeStr,playMinTimeStr,playSecondTimeStr]; } else{ totalTimeStr = [[NSString alloc] initWithFormat:@"%@:%@",playMinTimeStr,playSecondTimeStr]; } NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowBlurRadius = 4; shadow.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5]; shadow.shadowOffset =CGSizeMake(0,2); NSAttributedString * attributedText = [[NSAttributedString alloc] initWithString:totalTimeStr attributes:@{NSShadowAttributeName:shadow}]; _timeLabel.attributedText = attributedText; defaultImage = [UIImage imageNamed:@"uploadFile_Video"]; } else{ //iOS格式的图片 代理拿不到缩略图 if([filePath rangeOfString:@".HEIC"].location != NSNotFound ||[filePath rangeOfString:@".heic"].location != NSNotFound){ fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath]; } _timeLabel.hidden = YES; } fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [_imageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { // if(image){ // HLog(@"11图片1:%@",imageURL.absoluteString); // } // else{ // HLog(@"11图片0:%@",imageURL.absoluteString); // } }]; // if(!_isEditType){ // self.selectButton.hidden = YES; // } // else{ // self.selectButton.hidden = NO; // self.selectButton.selected = curFileModel.isSelectType; // } self.selectButton.selected = curFileModel.isSelectType; } #pragma mark 选中按钮 - (void)didClickButFun:(UIButton*)but { but.selected = !but.selected; if(_didClckSelectBut){ _didClckSelectBut(but.selected); } } - (void)longPress:(UILongPressGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { HLog(@"长按手势开始"); if(_didClickEditType && !_isEditType){ _didClickEditType(YES); } } } @end