Files
mier_ios/SweetParty/主类/Homepage/View/SPHomepagePhotoView.m
2025-08-11 10:43:19 +08:00

233 lines
9.0 KiB
Objective-C
Executable File

//
// SPHomepagePhotoView.m
// SweetParty
//
// Created by bj_szd on 2022/6/2.
//
#import "SPHomepagePhotoView.h"
#import "SPPickPhotoCell.h"
#import "MLNetWorkHelper.h"
#import "KNPhotoBrowser.h"
@interface SPHomepagePhotoView () <TZImagePickerControllerDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, KNPhotoBrowserDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isSelf;
@end
@implementation SPHomepagePhotoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.dataArray = [NSMutableArray arrayWithCapacity:0];
[self createUI];
}
return self;
}
- (void)setModel:(SPHomepageModel *)model {
_model = model;
if ([model.uid integerValue] == [BJUserManager.userInfo.uid integerValue]) {
self.isSelf = YES;
}
[self.dataArray removeAllObjects];
[self.dataArray addObjectsFromArray:model.user_albums_list];
[self.collectionView reloadData];
}
- (void)createUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
CGFloat itemW = (ScreenWidth-20*2-12)/2;
layout.itemSize = CGSizeMake(itemW, itemW);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 12;
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = kClearColor;
_collectionView.collectionViewLayout = layout;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerNib:[UINib nibWithNibName:@"SPPickPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"SPPickPhotoCell"];
[self addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(0);
}];
}
#pragma mark -------- collectionView代理方法 ------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.isSelf) {
if (self.dataArray.count < 6) {
return self.dataArray.count + 1;
}else {
return 6;
}
}else {
return self.dataArray.count;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SPPickPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SPPickPhotoCell" forIndexPath:indexPath];
if (indexPath.row < self.dataArray.count) {
SPHomepageAlbumModel *model = self.dataArray[indexPath.row];
cell.deleteBtn.hidden = YES;
[cell.imgV sd_setImageWithURL:[NSURL URLWithString:model.image]];
if (self.isSelf) {
cell.deleteBtn.hidden = NO;
cell.deleteBtn.tag = indexPath.row;
[cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
}
}else {
cell.imgV.image = [UIImage imageNamed:@"trend_pick_photo"];
cell.deleteBtn.hidden = YES;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= self.dataArray.count) {
[self pushTZImagePickerController];
}else {
[self onPreviewImage:indexPath.row];
}
}
#pragma mark - TZImagePickerController
- (void)pushTZImagePickerController {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6-self.dataArray.count columnNumber:4 delegate:self pushPhotoPickerVc:YES];
// 4. 照片排列按修改时间升序
imagePickerVc.sortAscendingByModificationDate = YES;
imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
imagePickerVc.showSelectedIndex = YES;
imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.allowPickingImage = YES;
imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[[self getCurrentVC] presentViewController:imagePickerVc animated:YES completion:nil];
}
#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
[self onUploadImages:photos];
}
-(void)onUploadImages:(NSArray *)photos {
NSString *urlStr = [NSString stringWithFormat:@"%@api/upload/img_upload", VERSION_HTTPS_SERVER];
NSDictionary *params = @{@"login_token":GVUSER.token};
[MLNetWorkHelper uploadImagesWithURL:urlStr parameters:params name:@"file[]" images:photos fileNames:nil imageScale:0.5 imageType:@"png" progress:nil success:^(id responseObject) {
NSArray *imgArr = responseObject[@"data"];
NSString *imgsUrl = [imgArr componentsJoinedByString:@","];
[self onAddPhotos:imgsUrl];
} failure:^(NSError *error) {
}];
}
-(void)onAddPhotos:(NSString *)imagesStr {
NSDictionary *params = @{@"image":imagesStr};
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/user/add_user_albums" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
[self onReloadAlbumData];
} Failure:^(id _Nonnull errorData) {
}];
}
- (void)deleteBtnClik:(UIButton *)sender {
if (sender.tag < self.dataArray.count) {
NSInteger index = sender.tag;
SPHomepageAlbumModel *model = self.dataArray[index];
NSDictionary *params = @{@"aid":model.aid};
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/user/delete_user_albums" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
[self.dataArray removeObjectAtIndex:index];
[self.collectionView reloadData];
} Failure:^(id _Nonnull errorData) {
}];
}
}
-(void)onReloadAlbumData {
NSDictionary *params = @{@"from_id":C_string(self.model.uid)};
[[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/player/player_home_page" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) {
SPHomepageModel *model = [SPHomepageModel mj_objectWithKeyValues:responseDic[@"data"]];
self.model = model;
} Failure:^(id _Nonnull errorData) {
}];
}
-(void)onPreviewImage:(NSInteger)index {
NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
for (SPHomepageAlbumModel *model in self.dataArray) {
KNPhotoItems *items = [[KNPhotoItems alloc] init];
items.url = model.image;
[mArr addObject:items];
}
KNPhotoBrowser *photoBrowser = [[KNPhotoBrowser alloc] init];
photoBrowser.itemsArr = [mArr copy];
photoBrowser.currentIndex = index;
photoBrowser.delegate = self;
photoBrowser.isNeedPageNumView = YES;
photoBrowser.isNeedRightTopBtn = YES;
[photoBrowser present];
}
- (void)photoBrowser:(KNPhotoBrowser *)photoBrowser rightBtnOperationActionWithIndex:(NSInteger)index{
KNActionSheet *actionSheet = [[KNActionSheet share] initWithTitle:@""
cancelTitle:@"取消"
titleArray:@[@"保存到相册"].mutableCopy
destructiveArray:@[].mutableCopy
actionSheetBlock:^(NSInteger buttonIndex) {
if (buttonIndex == 0) {
[UIDevice deviceAlbumAuth:^(BOOL isAuthor) {
if (isAuthor == NO) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]];
[[UIViewController currentViewController] presentViewController:alertController animated:YES completion:nil];
}else {
[photoBrowser downloadImageOrVideoToAlbum];
}
}];
}
}];
[actionSheet showOnView:photoBrowser.view];
}
- (void)photoBrowser:(KNPhotoBrowser *)photoBrowser
state:(KNPhotoDownloadState)state
progress:(float)progress
photoItemRelative:(KNPhotoItems *)photoItemRe
photoItemAbsolute:(KNPhotoItems *)photoItemAb {
if (progress >= 1) {
[HelpPageDefine showMessage:@"保存成功"];
}
}
@end