// // QXPhotosDetailVC.m // QXLive // // Created by dangkai on 2025/5/31. // #import "QXPhotosDetailVC.h" #import "QXMineNetwork.h" #import "QXImageCollectionViewCell.h" #import "QXPhotosDetailTopView.h" #import #import #import "UIImage+QX.h" #import "NSString+QX.h" #import "QXOSSManager.h" #import "YBImageBrowser/YBImageBrowser.h" #import "QXPhotosSectionView.h" #import "QXPhotosToolsView.h" #import "QXCreatPhotosViewController.h" #import "QXPhotosMoveListView.h" @interface QXPhotosDetailVC () @property(nonatomic,strong)QXPhotosDetailTopView *topView; @property(nonatomic,strong)UICollectionView *collectionView; @property(nonatomic,strong)UIButton *uploadBtn; @property(nonatomic,strong)QXPhotoDetailModel*model; /// 处理列表数组 @property(nonatomic,strong)NSMutableDictionary *photoDict; @property(nonatomic,strong)NSMutableArray *valueArray; // 上传相关数组 @property (nonatomic,strong)NSMutableArray *keyArray; @property (nonatomic,strong)NSMutableArray *uploadArray; @property (nonatomic,strong)NSMutableArray *urlArray; // 查看数组 @property (nonatomic,strong)NSMutableArray *selectedArray; // 选择处理 @property (nonatomic,strong)NSMutableArray *browserArray; @property (nonatomic,assign)BOOL isSelectedState; @property (nonatomic,assign)BOOL isMe; @property (nonatomic,strong)QXPhotosToolsView *toolsView; @end @implementation QXPhotosDetailVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; for (UIViewController*vc in self.navigationController.viewControllers) { if ([vc isKindOfClass:[NSClassFromString(@"QXCreatPhotosViewController") class]]) { [vc removeFromParentViewController]; } } } -(void)setNavgationItems{ [super setNavgationItems]; // self.navigationItem.title = self.name; } -(void)selectedAction:(UIButton*)sender{ sender.selected = !sender.selected; self.isSelectedState = sender.selected; if (!self.isSelectedState) { for (QXPhotoModel*md in self.selectedArray) { md.isSelected = NO; } [self.selectedArray removeAllObjects]; [UIView animateWithDuration:0.15 animations:^{ self.toolsView.y = SCREEN_HEIGHT; } completion:^(BOOL finished) { [self.toolsView removeFromSuperview]; }]; self.collectionView.contentInset = UIEdgeInsetsZero; }else{ MJWeakSelf [self.view addSubview:self.toolsView]; [UIView animateWithDuration:0.15 animations:^{ weakSelf.toolsView.y = SCREEN_HEIGHT-TabbarContentHeight; }]; self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, TabbarContentHeight, 0); } [self.collectionView reloadData]; } -(void)getData{ MJWeakSelf [QXMineNetwork photosDetailWithAlbumId:self.albumId pwd:self.pwd?self.pwd:@"" successBlock:^(QXPhotoDetailModel*model) { weakSelf.topView.model = model; if (weakSelf.page == 1) { [weakSelf.dataArray removeAllObjects]; [weakSelf.valueArray removeAllObjects]; [weakSelf.photoDict removeAllObjects]; [weakSelf.selectedArray removeAllObjects]; weakSelf.model = model; }else{ NSMutableArray *arr = [NSMutableArray arrayWithArray:weakSelf.model.image_list]; [arr addObjectsFromArray:model.image_list]; model.image_list = arr; weakSelf.model = model; } for (QXPhotoModel*md in model.image_list) { NSDate *date = [NSDate dateWithTimeIntervalSince1970:md.createtime.longLongValue]; //此处根据项目需求,选择是否除以1000 , 如果时间戳精确到秒则去掉1000 NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString*time = [formatter stringFromDate:date]; NSArray *keyArray = weakSelf.photoDict.allKeys; if ([keyArray containsObject:time]) { weakSelf.valueArray = [NSMutableArray arrayWithArray:[weakSelf.photoDict objectForKey:time]]; [weakSelf.valueArray addObject:md]; [weakSelf.photoDict setObject:[NSArray arrayWithArray:self.valueArray] forKey:time]; }else{ [weakSelf.valueArray removeAllObjects]; [weakSelf.valueArray addObject:md]; [weakSelf.photoDict setObject:[NSArray arrayWithArray:self.valueArray] forKey:time]; [weakSelf.dataArray addObject:time]; } } [weakSelf.collectionView reloadData]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } -(void)getPhotoList{ if (self.userId.length == 0) { return; } MJWeakSelf [QXMineNetwork photosListWithUserId:self.userId page:0 successBlock:^(NSArray * _Nonnull hotos) { weakSelf.photoList = hotos; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } - (void)initSubViews{ self.page = 1; self.topView = [[QXPhotosDetailTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 216)]; self.topView.delegate = self; [self.view addSubview:self.topView]; _isMe = [self.userId isEqualToString:[QXGlobal shareGlobal].loginModel.user_id]; self.topView.isMe = _isMe; if (!_isMe) { self.navigationItem.rightBarButtonItem = nil; self.uploadBtn.hidden = YES; }else{ UIButton*selectedBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)]; // [selectedBtn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)]; selectedBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentTrailing; [selectedBtn setTitle:QXText(@"选择") forState:(UIControlStateNormal)]; [selectedBtn setTitle:QXText(@"取消选择") forState:(UIControlStateSelected)]; selectedBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [selectedBtn setTitleColor:RGB16(0xFF8ACC) forState:UIControlStateNormal]; [selectedBtn addTarget:self action:@selector(selectedAction:) forControlEvents:(UIControlEventTouchUpInside)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn]; } UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 8; layout.minimumInteritemSpacing = 17; int itemWidth = (SCREEN_WIDTH-16*2-17*2)/3; layout.itemSize = CGSizeMake(itemWidth, itemWidth); layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16); layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40); self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.topView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-self.topView.bottom) collectionViewLayout:layout]; self.collectionView.backgroundColor = [UIColor clearColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[QXImageCollectionViewCell class] forCellWithReuseIdentifier:@"QXImageCollectionViewCell"]; [self.collectionView registerClass:[QXPhotosSectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QXPhotosSectionView"]; [self.view addSubview:self.collectionView]; [self.view addSubview:self.uploadBtn]; } #pragma mark - QXPhotosToolsViewDelegate -(void)didClickActionType:(QXPhotosToolsActionType)type{ switch (type) { case QXPhotosToolsActionShare: [self sharePhoto]; break; case QXPhotosToolsActionMove: [self movePhotoToOther]; break; case QXPhotosToolsActionDelete: [self deletePhoto]; break; default: break; } } -(void)sharePhoto{ if (self.selectedArray.count == 0) { showToast(@"请选择图片"); return; } if (self.selectedArray.count > 1) { showToast(@"分享图片不得超过1张"); return; } MJWeakSelf QXPhotoModel*md = self.selectedArray[0]; UIImageView *imgV = [[UIImageView alloc] init]; [imgV sd_setImageWithURL:[NSURL URLWithString:md.image] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { QXShareView *share = [[QXShareView alloc] init]; share.shareType = QXShareViewTypeSharePhoto; share.imageData = UIImageJPEGRepresentation(image, 0.7); [share showInView:weakSelf.view]; }]; } -(void)movePhotoToOther{ MJWeakSelf if (self.selectedArray.count == 0) { showToast(@"请选择图片"); return; } NSString *Id = @""; for (QXPhotoModel*md in self.selectedArray) { if (Id.length == 0) { Id = md.id; }else{ Id = [Id stringByAppendingFormat:@",%@",md.id]; } } QXPhotosMoveListView *listView = [[QXPhotosMoveListView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScaleWidth(400))]; listView.selecctedModelBlock = ^(QXPhotoModel * _Nonnull model) { [QXMineNetwork photosMoveWithNewAlbumId:model.id Id:Id successBlock:^(NSDictionary * _Nonnull dict) { weakSelf.page = 1; [weakSelf getData]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; }; [[QXGlobal shareGlobal] showView:listView popType:(PopViewTypeBottomToUpActionSheet) tapDismiss:YES finishBlock:^{ }]; } -(void)deletePhoto{ if (self.selectedArray.count == 0) { showToast(@"请选择图片"); return; } NSString *Id = @""; for (QXPhotoModel*md in self.selectedArray) { if (Id.length == 0) { Id = md.id; }else{ Id = [Id stringByAppendingFormat:@",%@",md.id]; } } MJWeakSelf [QXMineNetwork photosDeletePictureWithId:Id successBlock:^(NSDictionary * _Nonnull dict) { weakSelf.page = 1; [weakSelf getData]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } -(void)didClickEditWith:(QXPhotoDetailModel *)model{ MJWeakSelf QXCreatPhotosViewController *vc = [[QXCreatPhotosViewController alloc] init]; vc.createSuccessBlock = ^{ weakSelf.page = 1; [weakSelf getData]; }; vc.model = self.model; [self.navigationController pushViewController:vc animated:YES]; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return self.dataArray.count; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ NSString *key = self.dataArray[section]; NSArray *arr = self.photoDict[key]; return arr.count; } -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ QXImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXImageCollectionViewCell" forIndexPath:indexPath]; NSString *key = self.dataArray[indexPath.section]; NSArray *arr = self.photoDict[key]; QXPhotoModel*md = arr[indexPath.row]; cell.model = md; cell.selectedState = self.isSelectedState; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { QXPhotosSectionView *sectionView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QXPhotosSectionView" forIndexPath:indexPath]; NSString *key = self.dataArray[indexPath.section]; sectionView.titleLabel.text = key; return sectionView; } return nil; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.isSelectedState) { NSString *key = self.dataArray[indexPath.section]; NSArray *arr = self.photoDict[key]; QXPhotoModel*md = arr[indexPath.row]; md.isSelected = !md.isSelected; if (md.isSelected) { [self.selectedArray addObject:md]; }else{ [self.selectedArray removeObject:md]; } QXImageCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; cell.model = md; }else{ YBImageBrowser *browser = [YBImageBrowser new]; [self.browserArray removeAllObjects]; NSString *key = self.dataArray[indexPath.section]; NSArray *arr = self.photoDict[key]; QXPhotoModel*model = arr[indexPath.row]; NSInteger currentPage = 0; for (int i = 0 ;i < self.model.image_list.count ; i++) { QXPhotoModel*md = self.model.image_list[i]; YBIBImageData *data = [[YBIBImageData alloc] init]; data.imageURL = [NSURL URLWithString:md.image]; QXImageCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; data.projectiveView = cell.imageView; [self.browserArray addObject:data]; if ([md.id isEqualToString:model.id]) { currentPage = i; } } browser.dataSourceArray = self.browserArray; browser.currentPage = currentPage; [browser show]; } } -(void)uploadAction{ [self selectAlbumPhoto]; } -(void)selectAlbumPhoto { TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:4 delegate:nil pushPhotoPickerVc:YES]; imagePickerVc.allowCameraLocation = NO; imagePickerVc.allowPickingOriginalPhoto = NO; imagePickerVc.showSelectedIndex = YES; imagePickerVc.allowTakeVideo = NO; imagePickerVc.allowPickingVideo = NO; imagePickerVc.showSelectBtn = NO; imagePickerVc.allowCrop = NO; imagePickerVc.allowPickingGif = NO; MJWeakSelf [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { [weakSelf.uploadArray removeAllObjects]; [weakSelf.keyArray removeAllObjects]; for (int i = 0 ;i < assets.count ;i ++) { PHAsset*asset = assets[i]; [[TZImageManager manager] getOriginalPhotoDataWithAsset:asset completion:^(NSData *data, NSDictionary *info, BOOL isDegraded) { // NSString *imageType = [NSString contentTypeWithImageData:data]; // NSData *imgData = data; // if ([imageType isEqualToString:@"HEIC"] || [imageType isEqualToString:@"HEIF"] || [imageType isEqualToString:@"tiff"] || [imageType isEqualToString:@"webp"]) { UIImage *dataImg = [UIImage imageWithData:data]; NSData *imgData = UIImageJPEGRepresentation(dataImg, 0.5); // } [weakSelf.uploadArray addObject:imgData]; [weakSelf.keyArray addObject:[weakSelf imagePathWithKey:[NSString contentTypeWithImageData:imgData]]]; if (weakSelf.uploadArray.count == assets.count) { [weakSelf OSSUploadPhoto]; } }]; } }]; imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen; [imagePickerVc.navigationBar setBackgroundImage:[UIImage qx_imageWithColor:RGB16(0x333333)] forBarMetrics:UIBarMetricsDefault]; [self presentViewController:imagePickerVc animated:YES completion:nil]; } - (void)OSSUploadPhoto{ __weak typeof(self)weakSelf = self; showLoadingInView(self.view); [[QXOSSManager sharedInstance] activityUploadFile:self.uploadArray withObjectKey:self.keyArray isAsync:YES complete:^(NSArray *names, UploadImageState state) { dispatch_async_and_wait(dispatch_get_main_queue(), ^{ [weakSelf.urlArray removeAllObjects]; hideLoadingInView(weakSelf.view); if (state == UploadImageSuccess) { showToast(QXText(@"上传成功")); for (int i = 0; i < names.count; i++) { NSString *name = names[i]; NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,name]; [weakSelf.urlArray addObject:fileUrl]; } [weakSelf uploadImages]; }else{ showToast(QXText(@"上传失败")); } }); }]; } -(void)uploadImages{ if (self.urlArray.count == 0) { return; } MJWeakSelf showLoadingInView(self.view); NSString *images = [self.urlArray componentsJoinedByString:@","]; [QXMineNetwork addPhotosWithAlbumId:self.albumId images:images successBlock:^(NSDictionary * _Nonnull dict) { hideLoadingInView(weakSelf.view); weakSelf.page = 1; [weakSelf getData]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { hideLoadingInView(weakSelf.view); }]; } - (NSString *)imagePathWithKey:(NSString *)keyPath { NSString *fileBasePath = IMG_FILE_BASE_PATH; return [NSString stringWithFormat:@"%@%@/%@.%@",fileBasePath,[[QXOSSManager sharedInstance] currentDate],[NSUUID UUID].UUIDString,keyPath]; } -(NSMutableArray *)valueArray{ if (!_valueArray) { _valueArray = [NSMutableArray array]; } return _valueArray; } -(NSMutableArray *)uploadArray{ if (!_uploadArray) { _uploadArray = [NSMutableArray array]; } return _uploadArray; } -(NSMutableArray *)keyArray{ if (!_keyArray) { _keyArray = [NSMutableArray array]; } return _keyArray; } -(NSMutableArray *)urlArray{ if (!_urlArray) { _urlArray = [NSMutableArray array]; } return _urlArray; } -(NSMutableDictionary *)photoDict{ if (!_photoDict) { _photoDict = [NSMutableDictionary dictionary]; } return _photoDict; } -(NSMutableArray *)selectedArray{ if (!_selectedArray) { _selectedArray = [NSMutableArray array]; } return _selectedArray; } -(NSMutableArray *)browserArray{ if (!_browserArray) { _browserArray = [NSMutableArray array]; } return _browserArray; } -(UIButton *)uploadBtn{ if (!_uploadBtn) { _uploadBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-48-16, SCREEN_HEIGHT-86-48, 48, 48)]; [_uploadBtn setImage:[UIImage imageNamed:@"photos_upload"] forState:(UIControlStateNormal)]; [_uploadBtn addTarget:self action:@selector(uploadAction) forControlEvents:(UIControlEventTouchUpInside)]; } return _uploadBtn; } -(QXPhotosToolsView *)toolsView{ if (!_toolsView) { _toolsView = [[QXPhotosToolsView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, TabbarContentHeight)]; _toolsView.backgroundColor = [UIColor whiteColor]; _toolsView.delegate = self; } return _toolsView; } @end