Files
my_yuyin/QXLive/Mine(音域)/View/编辑/QXUserInfoEditFooterView.m
2025-09-22 18:48:29 +08:00

360 lines
16 KiB
Objective-C

//
// QXUserInfoEditFooterView.m
// QXLive
//
// Created by 启星 on 2025/5/20.
//
#import "QXUserInfoEditFooterView.h"
#import <TZImagePickerController/TZImageManager.h>
#import <TZImagePickerController/TZImagePickerController.h>
#import "UIImage+QX.h"
#import "NSString+QX.h"
#import "QXOSSManager.h"
#import "YBImageBrowser/YBImageBrowser.h"
@class QXUserInfoImageCell;
@interface QXUserInfoEditFooterView()<UICollectionViewDelegate,UICollectionViewDataSource,TZImagePickerControllerDelegate,QXUserInfoImageCellDelegate>
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *keyArray;
@property (nonatomic,strong)NSMutableArray *urlArray;
@property (nonatomic,strong)NSMutableArray *selectedAssets;
@property (nonatomic,strong)NSMutableArray *selectedPhotos;
@end
@implementation QXUserInfoEditFooterView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.maxCount = 6;
[self.urlArray addObject:@"mine_room_cover_add"];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 12, self.width-32, 24)];
NSString *maxStr = [NSString stringWithFormat:@"(%@)",QXText(@"最多上传6张")];
NSString *str = [NSString stringWithFormat:@"%@%@",QXText(@"背景图片"),maxStr];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
[attr yy_setFont:[UIFont systemFontOfSize:16] range:[str rangeOfString:QXText(@"背景图片")]];
[attr yy_setFont:[UIFont systemFontOfSize:12] range:[str rangeOfString:maxStr]];
[attr yy_setColor:QXConfig.textColor range:[str rangeOfString:QXText(@"背景图片")]];
[attr yy_setColor:RGB16(0x666666) range:[str rangeOfString:maxStr]];
self.titleLabel.attributedText = attr;
[self addSubview:self.titleLabel];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 20;
layout.minimumInteritemSpacing = 20;
int itemWidth = (self.width-16*2-20*2)/3;
layout.itemSize = CGSizeMake(itemWidth, itemWidth);
layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom+6, self.width, self.height-self.titleLabel.bottom-6) collectionViewLayout:layout];
self.collectionView.scrollEnabled = NO;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[QXUserInfoImageCell class] forCellWithReuseIdentifier:@"QXUserInfoImageCell"];
[self addSubview:self.collectionView];
}
-(void)setHideTitle:(BOOL)hideTitle{
_hideTitle = hideTitle;
if (hideTitle) {
self.titleLabel.hidden = YES;
self.collectionView.frame = CGRectMake(0, 12, self.width, self.height-24);
}
}
-(void)setImgs:(NSArray *)imgs{
_imgs = imgs;
[self.urlArray removeAllObjects];
[self.urlArray addObjectsFromArray:imgs];
if (_imgs.count < 6) {
[self.urlArray addObject:@"mine_room_cover_add"];
}
[self.collectionView reloadData];
}
-(void)setMaxCount:(NSInteger)maxCount{
_maxCount = maxCount;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.urlArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
QXUserInfoImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXUserInfoImageCell" forIndexPath:indexPath];
cell.imageUrl = self.urlArray[indexPath.row];
cell.delegate = self;
cell.index = indexPath.row;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *imageUrl = self.urlArray[indexPath.row];
if ([imageUrl hasPrefix:@"http"] || [imageUrl hasPrefix:@"https"]) {
if (self.imgs != nil) {
YBImageBrowser *browser = [YBImageBrowser new];
NSMutableArray *sourceArray = [NSMutableArray array];
for (NSString *url in self.urlArray) {
if ([url hasPrefix:@"http"] || [url hasPrefix:@"https"]) {
YBIBImageData *data = [[YBIBImageData alloc] init];
data.imageURL = [NSURL URLWithString:url];
QXUserInfoImageCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
data.projectiveView = cell.imageView;
[sourceArray addObject:data];
}
}
browser.dataSourceArray = sourceArray;
browser.currentPage = indexPath.row;
[browser show];
}else{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:self.selectedAssets selectedPhotos:self.selectedPhotos index:indexPath.item];
imagePickerVc.maxImagesCount = self.maxCount;
imagePickerVc.allowPickingGif = NO;
imagePickerVc.autoSelectCurrentWhenDone = NO;
imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.allowPickingMultipleVideo = NO;
imagePickerVc.showSelectedIndex = YES;
imagePickerVc.isSelectOriginalPhoto = NO;
imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
self->_selectedAssets = [NSMutableArray arrayWithArray:assets];
[self->_collectionView reloadData];
}];
[self.viewController presentViewController:imagePickerVc animated:YES completion:nil];
}
}else{
if (self.imgs != nil) {
self.maxCount = 7 - self.urlArray.count ;
}
[self selectAlbumPhoto];
}
}
-(void)didClickDelete:(NSString *)imageUrl index:(NSInteger)index{
if (self.imgs != nil) {
[self.urlArray removeObjectAtIndex:index];
if (![self.urlArray.lastObject isEqualToString:@"mine_room_cover_add"]) {
[self.urlArray addObject:@"mine_room_cover_add"];
}
[self.collectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickDelete:index:)]) {
[self.delegate didClickDelete:imageUrl index:index];
}
}else{
NSInteger index = [self.urlArray indexOfObject:imageUrl];
[self.dataArray removeObjectAtIndex:index];
[self.keyArray removeObjectAtIndex:index];
[self.selectedAssets removeObjectAtIndex:index];
[self.selectedPhotos removeObjectAtIndex:index];
[self.urlArray removeObjectAtIndex:index];
if (![self.urlArray.lastObject isEqualToString:@"mine_room_cover_add"]) {
[self.urlArray addObject:@"mine_room_cover_add"];
}
[self.collectionView reloadData];
}
}
-(void)selectAlbumPhoto {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:self.maxCount columnNumber:4 delegate:self pushPhotoPickerVc:YES];
imagePickerVc.maxImagesCount = self.maxCount;
imagePickerVc.allowCameraLocation = NO;
imagePickerVc.allowPickingOriginalPhoto = NO;
if (self.imgs == nil) {
if (self.selectedAssets.count>0) {
imagePickerVc.selectedAssets = self.selectedAssets;
}
}
imagePickerVc.showSelectedIndex = YES;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.showSelectBtn = NO;
imagePickerVc.allowCrop = NO;
imagePickerVc.allowPickingGif = NO;
MJWeakSelf
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
[weakSelf.dataArray 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.dataArray addObject:imgData];
[weakSelf.keyArray addObject:[weakSelf imagePathWithKey:[NSString contentTypeWithImageData:imgData]]];
if (weakSelf.dataArray.count == assets.count) {
[weakSelf OSSUploadPhoto];
}
}];
[self.selectedAssets removeAllObjects];
[self.selectedAssets addObjectsFromArray:assets];
[self.selectedPhotos removeAllObjects];
[self.selectedPhotos addObjectsFromArray:photos];
}
}];
imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[imagePickerVc.navigationBar setBackgroundImage:[UIImage qx_imageWithColor:RGB16(0x333333)] forBarMetrics:UIBarMetricsDefault];
[self.viewController presentViewController:imagePickerVc animated:YES completion:nil];
}
- (void)OSSUploadPhoto{
__weak typeof(self)weakSelf = self;
showLoadingInView(self.viewController.view);
[[QXOSSManager sharedInstance] activityUploadFile:self.dataArray withObjectKey:self.keyArray isAsync:YES complete:^(NSArray<NSString *> *names, UploadImageState state) {
dispatch_async_and_wait(dispatch_get_main_queue(), ^{
hideLoadingInView(weakSelf.viewController.view);
if (state == UploadImageSuccess) {
showToast(QXText(@"上传成功"));
if (self.imgs == nil) {
[weakSelf.urlArray removeAllObjects];
if (names.count < self.maxCount) {
for (NSString*fileName in names) {
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName];
[weakSelf.urlArray addObject:fileUrl];
}
[weakSelf.urlArray addObject:@"mine_room_cover_add"];
}else if (names.count == self.maxCount){
for (NSString*fileName in names) {
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName];
[weakSelf.urlArray addObject:fileUrl];
}
}else{
for (int i = 0; i < self.maxCount; i++) {
NSString *name = names[i];
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,name];
[weakSelf.urlArray addObject:fileUrl];
}
}
[weakSelf uploadImgs];
}else{
NSMutableArray *arr = [NSMutableArray array];
for (NSString*fileName in names) {
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName];
[weakSelf.urlArray insertObject:fileUrl atIndex:0];
[arr addObject:fileUrl];
}
[weakSelf uploadImgs:arr];
}
}else{
showToast(QXText(@"上传失败"));
}
});
}];
}
- (NSString *)imagePathWithKey:(NSString *)keyPath {
NSString *fileBasePath = IMG_FILE_BASE_PATH;
return [NSString stringWithFormat:@"%@%@/%@.%@",fileBasePath,[[QXOSSManager sharedInstance] currentDate],[NSUUID UUID].UUIDString,keyPath];
}
-(void)uploadImgs{
[self.collectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(didUploadFinishedWithImageUrlList:)]) {
NSMutableArray *arr = [NSMutableArray arrayWithArray:self.urlArray];
if ([arr containsObject:@"mine_room_cover_add"]) {
[arr removeObject:@"mine_room_cover_add"];
}
[self.delegate didUploadFinishedWithImageUrlList:arr];
}
}
-(void)uploadImgs:(NSArray*)imgs{
[self.collectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(didUploadFinishedWithImageUrlList:)]) {
[self.delegate didUploadFinishedWithImageUrlList:imgs];
}
}
-(NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (NSMutableArray *)keyArray{
if (!_keyArray) {
_keyArray = [NSMutableArray array];
}
return _keyArray;
}
-(NSMutableArray *)urlArray{
if (!_urlArray) {
_urlArray = [NSMutableArray array];
}
return _urlArray;
}
-(NSMutableArray *)selectedAssets{
if (!_selectedAssets) {
_selectedAssets = [NSMutableArray array];
}
return _selectedAssets;
}
-(NSMutableArray *)selectedPhotos{
if (!_selectedPhotos) {
_selectedPhotos = [NSMutableArray array];
}
return _selectedPhotos;
}
@end
@implementation QXUserInfoImageCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)setImageUrl:(NSString *)imageUrl{
_imageUrl = imageUrl;
if ([imageUrl hasPrefix:@"http"] || [imageUrl hasPrefix:@"https"]) {
self.deleteBtn.hidden = NO;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil];
}else{
if ([imageUrl isEqualToString:@"mine_room_cover_add"]) {
self.imageView.image = [UIImage imageNamed:imageUrl];
self.deleteBtn.hidden = YES;
}else{
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,imageUrl];
[self.imageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:nil];
self.deleteBtn.hidden = NO;
}
}
}
-(void)initSubviews{
self.imageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.imageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
[self.imageView addRoundedCornersWithRadius:10];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.right.equalTo(self.contentView);
}];
self.deleteBtn = [[UIButton alloc] init];
[self.deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.deleteBtn setImage:[UIImage imageNamed:@"Plus Circle"] forState:(UIControlStateNormal)];
[self.contentView addSubview:self.deleteBtn];
[self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.top.equalTo(self);
make.size.mas_equalTo(CGSizeMake(25, 25));
}];
}
-(void)deleteAction:(UIButton*)sender{
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickDelete:index:)]) {
[self.delegate didClickDelete:self.imageUrl index:self.index];
}
}
@end